NetBeans is a great open source Java IDE which I'm using to develop my home automation system.
It's great in that it's possible to run and debug code on the Raspberry Pi, which will be explained in much more detail in the different below sections.
NetBeans is now part of Oracle (not to the biggest amusement of the open source guys), but was once owned by Sun Microsystems.
It initially began as a student project at the university of Prague in the mid '90s. Later on, it was bought by Sun Microsystems.
And the rest... is history.
I'm using NetBeans 8.2 to create my Home Automation application. One of the major reasons is that there's a possibility to remotely debug on a Raspberry Pi.
I'm NOT using a higher version of NetBeanse, since they introduced some weird ways of working that don't fit the way I'm used to work.
Note: since a few weeks I'm using NetBeans 12.5 to create and maintain my home automation application. I found the issues there were and solved them accordingly.
See section Issues with NetBeans 12.5 and how to overcome them for more details.
Note that I since recently moved to NetBeans 17. See section NetBeans 17 below.
Since a while NetBeans 17 is out. Not that it matters a lot for my applications, but I think it's anyhow good to keep up with the latest (but therefore not greatest) version. Especially regarding security fixes, to name one.
However, when I first used NB17 to try to connect to my Raspberry Pi environment, I didn't see the remote platforms I created before with NB12.5 when I wanted to select the run environment (right-click on the project -> Properties -> Run -> Runtime Platform). The only platform I saw was the Project Platform.
But still, when I pressed the Manage Platforms... button (also reachable via Tools -> Java Platforms) I did see all the remote platforms. So, what was the issue here?
At first, I didn't really know. So I posted a question on the NB mailing list but no reaction after a few days. So, I went for a search on the internet and then I saw this very interesting page about using the Oracle Java SE Embedded Support in NetBeans IDE. Although I followed a similar page in the (long) past (I was using this mechanism already on NB7/8/12.5, right?) I found the solution to my problem.
It boils down to the fact that on my Windows machine, the default JDK used in NB17 was JDK17 while on my Raspberry Pi I used the Azul JDK11. When NB tries to lay down a connection with the remote device (Raspberry Pi in this case) it looks for the same JDK on the remote device as is used on the Windows machine. Since those were different (no match found) the remote sessions were not shown.
Solution: since I use JDK11 on my Raspberry Pi and I also had installed JDK11, next to other JDK versions, it was a matter of selecting the correct JDK in NB17 for my project and moved it to JDK11. After that change, I had my remote sessions back in the Runtime Platorm: dropdown list.
Open NetBeans 12.5 and select File -> New Project.
If not selected by default yet, make sure the following items are selecte:
For the Categories section: Java with Ant
For the Projects section: Java Application
Press Next
Give a project name and press Finish. The new project should be added to your current list of Projects.
If you created a new Java project in NetBeans, the next step is to connect the project to the Raspberry Pi so that we can run and/or debug the application on the Raspberry Pi.
To do so, make sure the following is in place:
Get a proper JDK version.
Depending on the type of project you want to create, a different Java version might be needed. For example, if you want to create a Java project based on the Pi4J 1.2 Java framework, then you need JDK1.8 installed on your Raspberry Pi.
A great website to get a proper JDK for the Raspberry Pi is the Azul website. Open the website and follow the below steps:
Select the button Download now
Move down the new page until the section Download Azul Zulu Builds of OpenJDK.
Select Arm 32-bit HF in the Architecture selection box. As of this writing, there will be 3 possible JDK's available:
Java 13 (MTS)
Java 11 (LTS)
Java 8 (LTS)
Let's take Java 8 as an example here, so download the corresponding .tar.gz file
Once downloaded, grab a tool to copy the tar file to a location on the Raspberry Pi (e.g. WinSCP on Windows)
Once the tar file is on the Raspberry Pi, untar using tar -zxvf <name_of_tar.gz_file>
Rename the untarred directory into jdk8u312 (again, this is the version as of this writing)
Move the directory to the /usr/lib/jvm location : sudo mv jdk8u312 /usr/lib/jvm
Make sure you have a .bash_extrapaths in your home directory that is called through ~/.bashrc when you open a terminal
Change the content of that file: make sure the path to /usr/lib/jvm/jdk8u312/bin is the first path in your overall PATH variable, like so:
PATH=/usr/lib/jvm/jdk8u312/bin:$PATH
PATH=/usr/sbin:$PATH
This way, when we run the command java -version we should have the following feedback:
pi@domohome:~ $ java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (Zulu 8.58.0.13-CA-linux_aarch32hf) (build 1.8.0_312-b07)
OpenJDK Client VM (Zulu 8.58.0.13-CA-linux_aarch32hf) (build 25.312-b07, mixed mode)
Once the JDK is in place, go back to the NetBeans IDE and select Tools -> Java Platforms in the menu bar.
Select Add Platform... and then Remote Java Standard Edition and fill in the following fields:
Give a name to the platform the project you're working on.
Type the IP address of the host
Type the user name of the host (here: pi). This will automatically propose a location for the Working Dir (last input field of the popup dialogue). You can change it if you want, or leave it as is.
Depending on the connection you want, you can choose between password or key authentication.
Let's choose for key authentication (then you must have a private/public key exchange in place for this Putty session!)
Point to the location of the private key file.
No need to fill in a passphrase, otherwise it won't be possible to connect automatically.
For the Remote JRE Path field: fill in the location where the Java SDK is installed, up until the jre path. In our example, this would be:
/usr/lib/jvm/jdk8u312/jre
Working Dir: as mentioned before, this is already pre filled the moment you pass a user name. Change it to whatever you want, if needed.
If all the above fields are filled in, the Finish button will become active. Press that button: a check will run to see if NetBeans can access the Raspberry Pi and generate thd working directory, based on the input given. If all goes fine, this should work.
As a last action, an overview will be shown of the newly created Java platform with all the fields filled in. Some of them will be editable, others will be readonly.
There will also be a Test Platform button which can be used at any time to test the connection between the NetBeans IDE and the Raspberry Pi platform.
If the above is working fine, you can go back to the project you started in NetBeans. Select the projedt and right-click on it. Then select Properties.
Once this window opens, select the Run section. In the new dialogue box, select the pull down box Runtime Platform and select the Java platform you just created. you will be forced to give it a new configuration name. Do this.
Press the OK button.
If you now press the Run icon in the NetBeans IDE, your Java application should be compiled and run on the Raspberry Pi.
If you now press the Debug icon in the NetBeans IDE and you put a breakpoint somewhere in your code (e.g. in the System.out.println() statement you might have added), then the debugger should stop at that line.
Note:
Make sure you have read and executed the section Issues with NetBeans 12.5 and how to overcome them first, otherwise you will have problems!!!!
When trying to use NetBeans 12.5 in combination with a Raspberry Pi to debug your application, there are some issues that have to be resolved.
The very first time you try to build on a new device, the build will fail. You will see the following error:
profile-rp-calibrate-key:
Connecting to 192.168.1.37:22
cmd : cd '/home/pi/mystuff/NetBeansProjects//MainTest'; '/home/pi/mystuff/azul_java/java_11/zulu11.60.19-ca-jdk11.0.17-
inux_aarch32hf/bin/java' -Xrunjdwp:transport=dt_socket,address=demosite.local:57787 -Dfile.encoding=UTF-8 -jar
home/pi/mystuff/NetBeansProjects//MainTest/dist/MainTest.jar
ERROR: transport error 202: getaddrinfo: unknown host
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [/home/ubuntu/workspace/zulu11/linux/armhf/c1/build/generic/ca/release/crossbuild/zulu11/src/jdk.jdwp.agent/share/native/libjdwp/debugInit.c:735]
E:\AppData\NetBeansProjects\LibreComputer\MainTest\nbproject\remote-platform-impl.xml:147: The following error occurred while executing this line:
E:\AppData\NetBeansProjects\LibreComputer\MainTest\nbproject\remote-platform-impl.xml:96: Remote command failed with exit status 2
BUILD FAILED (total time: 7 seconds)
In the Maven build process, there's a fixed address location where the debug build wants to start: demosite.local. So far, I couldn't find where that address is coming from, but I found a workaround.
The workaround is to make sure that the /etc/hosts file of the Raspberry Pi contains the following line (next to the existing lines):
127.0.0.1 demosite.local
Make sure this line is added to the end of the file.
Next time the debugger wants to "address" that weird address, the hosts file will reroute this to the local address 127.0.0.1 and all will work fine.
When you run the debug mode in NetBeans 12.5, you will encounter the following error message:
Error: Could not find or load main class args-line
This issue can be solved by adding the following line at the end in the build.xml file that is automatically generated when the project was created:
<property name="debug-args-line" value = ""/>
If all goes well, you should now be able to debug your Java application running on the Raspberry Pi.
Start by creating a new project in NetBeans. Point to a directory where you want your project started.
Next point is to add a few external libraries to the project. Therefore, select Tools -> Libraries and add a few libraries by clicking the button "New Library..." a few times.
Give a name to the directory, e.g. My Pi4J 1.2. Next, add for every tab on the right side the binaries (tab Classpath), sources (tab Sources) and documentation (tab Javadoc). You can give zip files, folders or JAR's.
For Pi4J, you can add the following files:
For the tab Classpath:
pi4j-core.jar
pi4j-device.jar
pi4j-gpio-extension.jar
All the above .jar files are generated when running the Maven compilation in Pi4J.
For the tab Sources:
pi4j-core-sources.jar
pi4j-device-sources.jar
pi4j-gpio-extension-sources.jar
Having the sources at hand is very comfortable if you want to step into a library method...
For the tab Javadoc:
pi4j-core-javadoc.jar
pi4j-device-javadoc.jar
pi4j-gpio-extension-javadoc.jar
Once this is in place, press the OK button.
Now you can right-click on the Libraries subfolder in the left project treeview. Select Add Library... and then select the My Pi4J 1.2 library just created and you will be able to use the functionality from that library.
Next to the Pi4J lib you also need the following libraries:
Slf4J 1.7.13
Slf4J Simple 1.7.13
MQTT 1.0.2
JSON simple 3.1.1