<< Summary

Java remote debugging and profiling on the Raspberry PI Raspberry PI

With the limited amount of RAM available on the Raspberry PI, there is just no way to run modern IDEs on it. It just does not make any sense.
People develop on another box - with more RAM - and eventually upload the compiled classes on the Raspberry PI, where they can be run.
And that works.
Until you need the debugging capabilities you are used to...

The Java Platform Debugging Architecture (JPDA) makes remote debugging possible.
All you need is to see the Raspberry PI on the network your IDE runs on, as we will demonstrate.
We will show how to achieve this from JDeveloper.
I use JDeveloper for all my PI4J developments, because:

We will use as an example the Battery Monitor application, its doc is avaibale here, its sources there.

We assume that you have compiled the classes you want to debug from your IDE (JDeveloper in this case), and uploaded the generated classes on the Raspberry PI.

Remote Debugging

On the Raspberry PI

You need to tweak the way java is started to take the remote debugger in account.
In the script named battery.monitor, modify the line that says:

  sudo java -cp $CP adc.sample.BatteryMonitor $*
        
into
  
  sudo java -client -agentlib:jdwp=transport=dt_socket,server=y,address=1044 -cp $CP adc.sample.BatteryMonitor $*
        
Notice the port, it is 1044. You can change that value at your leisure, as long as the port is available.
On the Raspberry PI, run the script, the application starts.

  Prompt> ./battery.monitor
  Read an ADC
  Listening for transport dt_socket at address: 1044

        

On the remote machine

In JDeveloper, in the project you want to debug, edit the project properties, and go to the Run/Debug section, and do check the Remote Debugging box.

Next go to the Tools Settings | Debugger | Remote section, and set the host and port appropriately.

Notice that raspberrypi is the name of the Raspberry PI on the network, and that 1044 is the port we used on the Raspberry PI to start the application.
In your code, set a couple of breakpoints, and start debugging:

You're prompted to confirm your options...

And that's it! The debugger stops when a breakpoint is hit. You can see - and change - the values of your variables, go step by step, and make anything the debugger allows you to do.

Such features exist in all Java IDEs. Just look for them if JDeveloper is not the one you use.
Happy debugging!

Profing, using JConsole

jconsole is a utility that comes with the Java Devlopment Ket (JDK). It can be used to profile a Java Virtual Machine (JVM) instance, locally, or remotely.
For the exact same reasons as the ones mentioned above about the debugging, we will run it remotely.

On the Raspberry PI

You need to add the following parameters when starting the JVM to monitor (i.e. on the RPi):

 JAVA_OPTIONS="$JAVA_OPTIONS -Dcom.sun.management.jmxremote.port=1234 \
                             -Dcom.sun.management.jmxremote.ssl=false \
                             -Dcom.sun.management.jmxremote.authenticate=false \
                             -Djava.rmi.server.hostname=raspberrypi-boat"
        
The port and the hostname need to match your environment, of course.
The other parameters should make sense, to whom it may concern...

On the remote machine

Now, on the remote machine, launch jconsole. It is in JAVA_HOME/bin/jconsole.

Click to enlarge
Connect...

Check the Remote Process radio button, and provide the hostame, and port (1234 in the example above).
The hit the Connect button.

Click to enlarge
Monitor

You can now see how the JVM is behaving.


Oliv did it