简体   繁体   中英

Remote Debugging GUI Java Application (swing) in NetBeans 8

I setup the remote debugging in NetBeans IDE between 2 Linux systems. Remote debugging an application that does not have a GUI works ok, but I am getting this error when I try to remote debug an application that has swing GUI:

Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it.

I would appreciate any suggestion!

NetBeans output window:

ant -f /home/toma/NetBeansProjects/GUIFormExamples -Dremote.platform.password=***** -Dremote.platform.rp.target=linux-15 -Dremote.platform.java.spec.ver=17 -Dremote.platform.rp.filename=linux -Ddebug.class=Antenna -Dnb.internal.action.name=debug debug-remote init: Deleting: /home/toma/NetBeansProjects/GUIFormExamples/build/built-jar.properties deps-jar: Updating property file: /home/toma/NetBeansProjects/GUIFormExamples/build/built-jar.properties compile: Copying 1 file to /home/toma/NetBeansProjects/GUIFormExamples/build Copy libraries to /home/toma/NetBeansProjects/GUIFormExamples/dist/lib. To run this application from the command line without Ant, try: java -jar "/home/toma/NetBeansProjects/GUIFormExamples/dist/GUIFormExamples.jar" jar: Connecting to 192.168.1.122:22 Connecting to 192.168.1.122:22 cmd : mkdir -p '/home/toma/NetBeansProjects//GUIFormExamples/dist' Connecting to 192.168.1.122:22 done. profile-rp-calibrate-passwd: Connecting to 192.168.1.122:22 cmd : cd '/home/toma/NetBeansProjects//GUIFormExamples'; '/usr/lib/jvm/j2sdk1.7-oracle/jre/bin/java' -Xdebug -Xrunjdwp:transport=dt_socket,address=localhost:39245 -Dfile.encoding=UTF-8 -jar /home/toma/NetBeansProjects//GUIFormExamples/dist/GUIFormExamples.jar Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it. at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207) at java.awt.Window.(Window.java:535)

You need to add DISPLAY environment variable export to your ant build xml file. I have blogged about this issue and it's solution on my blog

For my project there was a target named "-copy-to-remote-platform" and two macros in it: "runwithpasswd" and "runwithkey" in ANT build xml file which required some modifications.

I have added "export DISPLAY=:0;" to last sshexec commands in each of the aforementioned macros so that they both looked like this:

<sshexec host="${remote.platform.host}" port="${remote.platform.port}" username="${remote.platform.user}" password="${remote.platform.password}" trust="true" usepty="true"
                    command="export DISPLAY=:0; cd '${remote.project.dir}'; ${remote.platform.exec.prefix}'${remote.java.executable}' @{additionaljvmargs} -Dfile.encoding=${runtime.encoding} ${run.jvmargs} ${run.jvmargs.ide} -jar ${remote.dist.jar} ${application.args}"/>

Mind the "export DISPLAY=:0;" on the beginning of the "command" attribute.

You need to use X11 forwarding, to make the GUI on the remote computer visible on your computer (or otherwise give a valid DISPLAY environment variable, so the GUI can be displayed somewhere ). This is a Linux configuration issue, Java is just complaining that it can't create a GUI, because (as far as it knows) there is no screen available.

Success! It is not as easy as it should be, but it works.

In NetBeans (I used version 8) create a new Java Platform for remote debugging: Tools -> Java Platforms -> Add Platform -> Remote Java Standard Edition -> ... (for more info see this link: https://netbeans.org/kb/docs/java/javase-embedded.html ). Press the drop-down menu on the Debug Icon (debug-remote) and watch the output window.

That works well if the program does not have a GUI. If the program has a GUI, I get this error: "java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it"

Even if the program has a GUI, the steps above help, because it automatically deploys your program on the remote server.

To debug the GUI, the workaround I found, is to remotely connect to the server using ssh or VNC and run the java program in debug mode:

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y -jar "/MyPath/Program.jar".

Java VM should pause the program and wait for NetBeans debugger to connect.

In NetBeans, set a break point in the program and attach to the remote process: Debug -> Attach Debugger -> Java Debugger, SocketAttach, dt_socket, IP address of the server, Port: 4000 -> OK

Later on you can just press the drop-down menu on the Debug Icon (Attach to ...) to start debugging.

The program should run to the break point and pause execution. You should have full control over the GUI on the programming computer but the program is executed on the server.

This is very useful when debugging Java programs on single board computers like BeagleBone Black or Raspberry PI that do not have enough horsepower to run NetBeans. This is essential when the single board computer is used in a robotics application and it needs to receive sensor inputs and control motors.

The Solution:

  1. Go to Run → Set Project Configuration → Customize..
  2. Click Manage Platforms
  3. Select the remote configuration for your RPI
  4. On right sight go to Exec Prefix and write startx in it.

Problem is that NetBeans single-quotes everything that you put into the Exec Prefix field. So you can put in your own single-quotes to construct a valid bash command. Imagine you have a VNC virtual desktop at display :2.0, you can trick it out with export' DISPLAY=:2.0;'sudo Finally this will result in a working bash command string with a quoted export and a quoted sudo (which does no harm). If you don't want your program to run in superuser mode, change it to export' DISPLAY=:2.0;sudo -u 'pi I found only the sudo command as a working way to cope with these single quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM