Using NetBeans to program and edit your code on the Raspberry pi
In the past, I've used NetBeans 8.2 to create and debug my project running on the Raspberry Pi. The development was done on my Windows PC and the debugging was running on the Raspberry Pi using jdb communication.
Recently, however, my NetBeans 8.2 went coco loco for a reason unknown to me and I had to restart my search for debugging the code on the RPi from within "a" NetBeans IDE.
As of this writing, NetBeans 12.5 is the latest release of this IDE.
add 127.0.0.1 demosite.local to /etc/hosts on the RPi
Reason:
When launching the debug session, Maven reads the file remote-platform-impl.xml which is part of the project you're running (located in .\<project>\nbproject). Along the way, it adds somewhere "demosite.local" to the <jpda.address> variable in that file and I have no idea where this is coming from so far.
The address should normally be <localhost> or <127.0.0.1>, but I didn't find a place to add this to the build mechanism.
So, the solution was to let the /etc/hosts do the translation; that is to say: map 'demosite.local' on IP address 127.0.0.1. Hence...
If you don't do this, this is what you will get as error when launching the debugger:
Connecting to 192.168.1.30:22
cmd : cd '/home/pi/mystuff/NetBeansProjects//Pi4JGpioControl'; '/usr/lib/jvm/java-11-openjdk-armhf/bin/java' -Xrunjdwp:transport=dt_socket,address=demosite.local:60465 -Dfile.encoding=UTF-8 -jar /home/pi/mystuff/NetBeansProjects//Pi4JGpioControl/dist/Pi4JGpioControl.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 [./src/jdk.jdwp.agent/share/native/libjdwp/debugInit.c:735]
E:\AppData\NetBeansProjects\Pi4JGpioControl\nbproject\remote-platform-impl.xml:147: The following error occurred while executing this line:
E:\AppData\NetBeansProjects\Pi4JGpioControl\nbproject\remote-platform-impl.xml:96: Remote command failed with exit status 2
BUILD FAILED (total time: 9 seconds)
add <property name="debug-args-line" value = ""/> to build.xml located in .\<project>.
If you don't have this line in the build.xml file, you will get the following error when trying to debug your application on the RPi (you won't have this issue if you just run your app on the RPi in the non-debug mode):
profile-rp-calibrate-key:
Property "debug-args-line" has not been set
Connecting to 192.168.1.30:22
cmd : cd '/home/pi/mystuff/NetBeansProjects//Pi4J_V2_Test'; '/usr/lib/jvm/java-11-openjdk-armhf/bin/java' ${debug-args-line} -Xrunjdwp:transport=dt_socket,address=demosite.local:58571 -Dfile.encoding=UTF-8 -jar /home/pi/mystuff/NetBeansProjects//Pi4J_V2_Test/dist/Pi4J_V2_Test.jar
Error: Could not find or load main class args-line
Caused by: java.lang.ClassNotFoundException: args-line
E:\AppData\NetBeansProjects\Pi4J_V2\Pi4J_V2_Test\nbproject\remote-platform-impl.xml:147: The following error occurred while executing this line:
E:\AppData\NetBeansProjects\Pi4J_V2\Pi4J_V2_Test\nbproject\remote-platform-impl.xml:96: Remote command failed with exit status 1
BUILD FAILED (total time: 22 seconds)
This is necessary since the build environment of Netbeans 12.5 requires this. Don't ask me why...
Below is the flow given in different collapsible sections on how to set up NetBeans 12.5 to be able to remotely debug on the RPi. The code itself is compiled on the Windows PC but when pressing the run or debug button in NetBeans, the code is transferred to the RPi and a run or debug session is started.
But before you're able to do that, the below steps have to be followed.
To begin with, select Tools -> Java Platforms to create a new platform.
When you're in the platform overview you will see all the existing local JDK's and also the remotely defined ones. Initially, this list will be empty but in my case there's already some history.
When you select a platform, you can see a lot of details about that platform.
For the remote ones you see the hostname, host IP address, port, user name, key file (if any), installation folder of the Java SE on the remote location and so on. On the left, you can see a snapshot of those details.
When you use the scrollbar on the right, you will see even more details, much of them automatically filled in by the remote plugin (see further).
The first step in adding a new Java platform is to select which platform you want to install. By default the Java Standard Edition platform is selected but this time we want to select a remote Java SE.
So, make sure you select the Remote Java Standard Edition bullet, like given on the right.
Once you pressed the Next button in the previous step, you will end up in the section Set Up Remote Platform.
Now is the time to provide the following items:
The platform name: this is the name that you will use later on to refer to the platform you want to select.
Maybe best not to add spaces in the name although I don't know if that really matters.
The host: this is the IP address the RPi has in the network.
The port: since we're using SSH to connect to the RPi, the default port number is proposed, being 22. AFAICS, there's no need to change this immediately, especially if you're working intranet wise instead of internet wise.
The password authentication:
Here you have two options:
using a password
using a key authentication
I've opted for a key authentication since this has a few important advantages:
since all has to go automatically, I don't want to provide each and every time a password when I want to start a debug session
I'm using already a private/public key mechanism to log in into my RPi, so I have already the mechanism in place
Remote JRE path: this is the location on the RPi where the Java installation can be found. You should only provide the path until the Java installation itself. From there onwards, the NetBeans remote debugger plugin can find all the stuff it needs.
Working Dir: this is the location where the remote debug plugin can copy the project to. Make sure write access is granted to that location.
Once all the above is done, press the Finish button. Once you press this button, NetBeans will start a communication with the RPi using the data you provided in the platform setup section here.
... is in the eating!
So, when all goes well, you should end up with an overview of the new Java platform created, like given on the right side here.
I've highlighted the important sections. That are the sections that you have to give your input for, which has happened in the previous step.
Some items cannot be changed anymore, like the display name or the install folder. If you want to change those, you have to create a new Java platform. This is how NetBeans has decided to set it up.
There are also some items that can be changed afterwards. An example is the working directory.
As you can see here, the working directory is set to /home/pi/NetBeansProjects/. There's no need for the last forward slash in the path, since NetBeans implicitly adds one while transferring the project to the RPi. It's not harmful, but you will see two forward slashes in the complete project path on the RPi.
Should you decide to change this, it can still be done here.
In short: all the greyed out parts are not changeable anymore, al the other parts are.
There's also a Test Platform button available which allows you to test the connection at any time. This can come in handy at times.
When you press the button, a kind of progress bar will pop up: Verifying Remote Platform.
After some time, if all goes well, you should see an information window that confirms there's a successful connection established with the remote platform.
Once all the above steps are successfully executed, you're now ready to use the newly created platform in your next projects. We'll see shortly how this is done.