Remote Debugging GUI Java Application (swing) in NetBeans 8 - java

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:
Go to Run → Set Project Configuration → Customize..
Click Manage Platforms
Select the remote configuration for your RPI
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.

Related

How do I set a X11 Display [duplicate]

I am trying to install a Java application on my Linux machine (Slackware).
I have received the following error, and I do not understand it.
Could you advise me how to approach the problem? Thank you.
Here is what I get: (I see that some X11 DISPLAY variable needs to be set, but what value should I give it and how?)
~$ java -jar gate-5.0-beta1-build3048-installer.jar
- ERROR -
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(Graphic sEnvironment.java:159)
at java.awt.Window.<init>(Window.java:407)
at java.awt.Frame.<init>(Frame.java:402)
at net.sourceforge.mlf.metouia.borders.MetouiaDotsBuf fer.<init>(MetouiaDotsBuffer.java:105)
at net.sourceforge.mlf.metouia.borders.MetouiaDots.<i nit>(MetouiaDots.java:66)
at net.sourceforge.mlf.metouia.borders.MetouiaToolBar Border.<init>(MetouiaToolBarBorder.java:49)
at net.sourceforge.mlf.metouia.MetouiaLookAndFeel.ini tComponentDefaults(MetouiaLookAndFeel.java:241)
at javax.swing.plaf.basic.BasicLookAndFeel.getDefault s(BasicLookAndFeel.java:130)
at javax.swing.plaf.metal.MetalLookAndFeel.getDefault s(MetalLookAndFeel.java:1591)
at javax.swing.UIManager.setLookAndFeel(UIManager.jav a:537)
at javax.swing.UIManager.setLookAndFeel(UIManager.jav a:581)
at com.izforge.izpack.installer.GUIInstaller.loadLook AndFeel(GUIInstaller.java:373)
at com.izforge.izpack.installer.GUIInstaller.<init>(G UIInstaller.java:116)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:30
at com.izforge.izpack.installer.Installer.main(Instal ler.java:62)
If you're on the main display, then
export DISPLAY=:0.0
or if you're using csh or tcsh
setenv DISPLAY :0.0
before running your app.
Actually, I'm surprised it isn't set automatically. Are you trying to start this application from a non-graphic terminal? If not, have you modified the default .profile, .login, .bashrc or .cshrc?
Note that setting the DISPLAY to :0.0 pre-supposes that you're sitting at the main display, as I said, or at least that the main display is logged on to your user id. If it's not logged on, or it's a different userid, this will fail.
If you're coming in from another machine, and you're at the main display of that machine and it's running X, then you can use "ssh -X hostname" to connect to that host, and ssh will forward the X display back. ssh will also make sure that the DISPLAY environment variable is set correctly (providing it isn't being messed with in the various dot files I mentioned above). In a "ssh -X" session, the DISPLAY environment variable will have a value like "localhost:11.0", which will point to the socket that ssh is tunnelling to your local box.
you must enable X11 forwarding in you PuTTy
to do so open PuTTy, go to Connection => SSH => Tunnels and check mark the Enable X11 forwarding
Also sudo to server and export the below variable here IP is your local machine's IP
export DISPLAY=10.75.75.75:0.0
Are you running this from within an X11 environment? You can use a terminal window, but it has to be within X (either after a graphical login, or by running startx).
If you're already within a graphical environment, try export DISPLAY=:0 for bash like shells (bash, sh, etc) or setenv DISPLAY :0 for C shell based shells (csh, tcsh, etc)
If you've connected from another machine via SSH, you use the -X option to display the graphical interface on the machine you're sitting at (provided there's an X server running there (such as xming for windows, and your standard Linux X server).
There are many ways to do this. I did something below convenient to me and always works fine.
On your remote server, make sure to install xorg-x11-xauth, xorg-x11-font-utils, xorg-x11-fonts.
Run the Xming Server on you local desktop
On putty, before ssh to the server, enable the X11 forwarding and set the display location to localhost:0.0
On the server, .Xauthority file is generated and notice that the DISPLAY variable is already set.
$ xauth list
$ xauth add
To test it, type xclock or xeyes
Note: To switch user, copy the .Xauthority file to the home directory of the respective user and also export the DISPLAY variable from that user.
One more thing that might be the problem in a case similar to described - X is not forwarded and $DISPLAY is not set when 'xauth' program is not installed on the remote side. You can see it searches for it when you run "ssh -Xv ip_address", and, if not found, fails, which's not seen unless you turn on verbose mode (a fail IMO). You can usually find 'xauth' in a package with the same name.
Very Easy, Had this same problem then what i did was to download and install an app that would help in displaying then fixed the error.
Download this app xming:
http://sourceforge.net/project/downloading.php?
Install, then use settings on this link:
http://www.geo.mtu.edu/geoschem/docs/putty_install.html or follow this steps:
Installing/Configuring PuTTy and Xming
Once PuTTy and Xming have been downloaded to the PC, install according to their respective instructions.
Configuring Xming
Once Xming is installed, run the application called 'XLaunch' and verify that the settings are as shown:
select Default entries on Display Settings windows, click next
click next on Session Type window.
click next on Additional parameters window(Notice clipboard checkbox is true)
save configuration and click to finish.
Configuring PuTTy
After installing PuTTy, double-click on the PuTTy icon on the desktop and configure as shown:
This shows creating a login profile then saving it.
On ssh -> X11, click on checkbox to enable X11 forwarding.
on X display location textbox, type localhost:0.0
save profile then connect remotely to server to test.
Cheers!!!
For those who are trying to get an X Window application working from Windows from Linux:
What worked for me was to setup xming server on my windows machine, set X11 forwarding option in putty when I connect to the linux host and put in my windows ip address with the display port and then the display variable with my windows IP address:0.0
Dont forget to add the linux hosts IP address to the X0.hosts file to ensure that the xming server accepts traffic from that host. Took me a while to figure that out.
I have had the same issue in Ubuntu 14.04.01 when I tried to install JDK 8 and Netbeans if I launch the script inside a Byobu terminal (maybe with Screens happens the same).
Just exit Byobu and (in a graphical terminal) run the script.
Initial Check.
1) When you are exporting the DISPLAY to other machine, ensure you entered the command xhost + on that machine. This command allows to other machine to export their DISPLAY on this machine. There may be security constraints, just know about it. Need to check ssh -X MachineIP will not require xhost + ?
2) Some times JCONSOLE won't show all its process, since those JVM process may run with different user and you are exporting the DISPLAY with another user. so better follow CD_DIR>sudo ./jconsole
3) In WAS (WEBSPHERE); jconsole won't be able to connect its java server process, that time just go till the link, then try connecting it. This worked for me. May be this page is initializing some variables to enable jconsole to connect with that server.
WAS console > Application servers > server1 > Process definition > Java Virtual Machine
I have faced the same issue with AIX (where command line interface only available, There is no DISPLAY UI) machine. I resolved by installing
NX Client for Windows
Step 1: Through that Windows machine, I connected with unix box where GUI console is available.
Step 2: SSH to the AIX box from that UNIX box.
Step 3: set DISPLAY like "export DISPLAY=UNIXMACHINE:NXClientPORTConnectedMentionedOnTitle"
Step 4: Now if we launch any programs which requires DISPLAY; it will be launched on this UNIX box.
VNC
If you installed VNC on UNIX box where display is available; then Windows and NX Client is not required.
Step 1: Use VNC to connect with Unix box where GUI console is available.
Step 2: SSH to the AIX box from that UNIX box.
Step 3: set DISPLAY like "export DISPLAY=UNIXMACHINE:VNCPORT"
Step 4: Now if we launch any programs which requires DISPLAY; it will be launched on this UNIX box.
ELSE
Step 1: SSH to the AIX box from that UNIX box.
Step 2: set DISPLAY like "export DISPLAY=UNIXMACHINE:VNCPORT"
Step 3: Now if we launch any programs which requires DISPLAY; it will be launched on this UNIX box.
Set the display mode is fine and also make sure that the apache Jmeter start from your USER mode, DON'T run in ROOT user.
$ cd /home/USER/apache-jmeter-5.5/
$ ./jmeter
There are two possibilities for it if your app has GUI or not:
1-) If your java app has GUI, firstly ensure that you have installed jdk package normally (non-headless). For example if you installed this:
sudo apt install openjdk-19-jre-headless
remove it:
sudo apt remove openjdk-19-jre-headless
then install the normal version:
sudo apt install openjdk-19-jre
Simply headless is using for GUIless apps. Further information you can look here: Difference between openjdk-6-jre, openjdk-6-jre-headless, openjdk-6-jre-lib
Then, either try to set DISPLAY variable as:
export DISPLAY=:0
or if it does not work (then, probably you have multiple monitors), set as:
export DISPLAY=:1
then run your jar file:
java -jar gate-5.0-beta1-build3048-installer.jar
2-) If your java app has no GUI, while you are running the app, you can try java.awt.headless=true flag as:
java -Djava.awt.headless=true -jar gate-5.0-beta1-build3048-installer.jar
Don't forget to execute "host +" on your "home" display machine, and when you ssh to the machine you're doing "ssh -x hostname"

how to start a java application system-independent at Windows startup

I want to automatically run a Java application under Windows at startup time, but not depending on a specical Windows or a special JRE-version.
Generally, a good way to run programs at Windows login time would be to add a registry entry under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
But it seems as if the system PATH is not evaluated when Windows tries to run the programs listed in the registry's RUN section.
So these both entries do work:
"C:\Program Files\Java\jdk1.8.0_111\bin\javaw" -jar c:\path\to\java.jar
"C:\ProgramData\Oracle\java\javapath\javaw" -jar c:\path\to\java.jar
But this one doesn't come up:
javaw -jar c:\path\to\java.jar
Now, the latter works when manually entered in a console window because there the required PATH exists and is evaluated:
PATH=....;C:\ProgramData\Oracle\Java\javapath;....
Now, I want to have the registry entry added from an automated installer programm and I would like to have it formed "windows-system-independent" (from Vista up to Win 10, and furthermore not depending from the version of the installed JRE). So I do not know a general path to the JRE and therefore I cannot add a specific RUN-command to the registry.
Does anyone know how to add JRE-calls system-independent correctly to the registry's RUN-section?
I've already tried with a command-file, e.g. "startup.cmd" with content java -jar file.jar there, have added a call of this file to the registry, and yes that worked, java was found, the application was started, but when using this method I have an annoying black command terminal window staying open all the time while the java app is running.
Thanx,
Tombo
Change your startup.cmd, from
java -jar file.jar
to
start java -jar file.jar
This will launch the java.exe in a new process an let your batch executor terminate.

Command stops whenever I log out of my server

I have a java program (text-based no GUI) that I have written and compiled and uploaded to a server.
I run it with java -cp myjar.jar mypackage.MyClass which starts it running processing a datafile with 20,000,000+ entries in it and printing output to System.out. I have calculated that it will take a very long time to process the data and I didn't want to have my laptop open for the 10 days of number crunching...
When I log out of my shell however, the process stops.
How can I execute the command and log out without it stopping? What is that even called?
I am using an Amazon Ubuntu EC2 server. I log in using a certificate from Mac OSX with terminal. The server seems to be using a bash shell.
Hope someone can help me out!
Jason.
Consider using screen instead of nohup. It allows you to create a virtual terminal that persists even after you logout/disconnect. When you reconnect to the server, you can immediately jump into the screen session you last had open.
Typical workflow on the server:
type screen (you may need to press space to leave intro page)
type in your command that you want to leave long-running (your java program, or an OS upgrade)
press ctrl a+d to leave screen (make sure to hold ctrl down)
To re-enter screen just use screen -r, and you will see the previous terminal and any running programs as you left it.
You can use nohup
nohup java -cp myjar.jar mypackage.MyClass > yourLogFile.log &
-----> http://ss64.com/bash/nohup.html

running java on linux server

I would like to run a jar file extracted from my java project to be run on a Linux server I connect through SSH Tunneling. There are few problems, first there is something wrong with the Display: I get the error
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:173)
at java.awt.Window.<init>(Window.java:437)
at java.awt.Frame.<init>(Frame.java:419)
at java.awt.Frame.<init>(Frame.java:384)
at javax.swing.JFrame.<init>(JFrame.java:174)
at random_v2.v2Frame.<init>(v2Frame.java:127)
at random_v2.Wrapper.main(Wrapper.java:25)
and second is that I am not sure if I have to install other applications as well. In my code, the java program needs to get run other applications like weka, do I have to install weka with the same directory name and specs that is in my mac?
I appreciate your help in advance.
Best wishes
Assuming you're tunneling into this unix box using PuTTY:
Make sure X11 forwarding is enabled in your PuTTY settings.
Connect via command line using this command:
ssh -X user#server
The -X option enables X11 forwarding.

Using Bumblee (Graphics Drivers) with Eclipse IDE [duplicate]

Does anyone know how to make eclipse or netbeans use the graphics card in optimus laptops by invoking optirun (bumblebee) inside the IDE so that one can just use the run button in the IDE to run the program in a graphics card within the IDE.
In simplest form I just want the IDE to do the equivalent of optirun ./javaproject
The way I did this in Eclipse was to first start the Java debugger jdwp and listen to a port. Then start the JVM with optirun java ... and use jdwp to connect to this port. Both tasks can be started at the same time in Eclipse by creating a Launch Group in the debug configuration settings (Run -> Debug Configurations). In detail:
Create a Remote Java Application debug configuration with "Standard (Socket Listen)" Connection Type and some arbitrary port, e.g. 56789. This attaches the Java debugger jdwp on port 56789 to a virtual machine which accepts debug connections at this port.
Now we need to start a JVM with optirun. This can be done with a External Tool Configuration (Run -> External Tools -> External Tool Configurations). Create a new Program configuration in the left side of the External Tools Configurations window. You could directly start optirun java <additional arguments> by filling in the required fields. However, I have decided to use a shell script which is reusable by different projects (As can be seen below, there is one part missing to make it entirely reusable. I'm glad for any help from more experienced Eclipse users...). Hence, the Location field points to this shell script. The script itself accepts three arguments: the classpath for the project, the name of the Java executable, and the port number. These arguments can be passed to the script in the Arguments field of the Main tab, e.g.
${project_classpath:${selected_resource_name}}
ExecName
56789
The shell script looks like this, assuming optirun is in your PATH:
#!/bin/sh
CLASS_PATH=${1}
JAVA_EXECUTABLE=${2}
PORT=${3}
# TODO: fix this java library path: pass it as an argument as well. Is there an Eclipse variable which stores this?
JAVA_LIBRARY_PATH=/usr/local/share/OpenCV/java
#------------------------------------------------------------------------------
optirun ${JAVA_BIN} -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:${PORT} -Djava.library.path=${JAVA_LIBRARY_PATH} -Dfile.encoding=UTF-8 -classpath ${CLASS_PATH} ${JAVA_EXECUTABLE}
#------------------------------------------------------------------------------
Finally, the two pieces are brought together in a Launch Group in the Debug Configurations window (Run -> Debug Configurations). Create a new Launch Group and add the two previously generated Debug configurations by clicking on Add in the Launches tab and by selecting the appropriate configurations.
Note that due to the classpath variable in step 2 (i.e. ${project_classpath:${selected_resource_name}}), the appropriate package needs to be selected in the Package Explorer before clicking on the run debug configuration button (make sure that the Launch Group is selected).
This solution works perfectly for me: I can debug Java code inside Eclipse which calls native code involving CUDA optimizations and Bumblebee only activates the discrete graphics card when necessary.
Just use optirun to start the IDE. For example, optirun eclipse or optirun netbeans
I build the project in Netbeans (F11) and run the following in a terminal:
optirun java -jar path/to/javaproject/dist/javaproject.jar
Mind that if you have any java parameters in your project, you need to add it manually. My workflow is like this:
Locate the Java options from the project, open Project -> Properties, Run. At VM Options I see -Djava.library.path=lwjgl/native/windows;:lwjgl/native/linux. I also have some parameters that I want to pass to main(String[]). With this information, I open a terminal and run:
cd path/to/javaproject
optirun java -Djava.library.path=lwjgl/native/windows;:lwjgl/native/linux \
-jar dist/javaproject.jar some paremeters
Another hint, if you have to open and close the program frequently, run optirun bash in a different tab so that preparing the use of the graphics card becomes faster. Alternatively, you can run optirun netbeans, but that means that the nvidia card will always be on even if you are programming which increases power use and increase the heat.
Important: if you are using a 32-bit JVM or Java libraries on a 64-bit machine, you also need to install the 32-bit drivers and libraries. For Ubuntu, the nvidia package already contains 32-bit drivers, see this answer. For other distros, you likely need to install lib32-* packages for Mesa, VirtualGL and nvidia-utils.
You can also rename java to java_real and use this portion of code as your java command :
#!/bin/bash
path=$(dirname $(readlink -f $0))
args=""
runner="$path/java_real"
for var in "$#"
do
if [ "$var" = "-3d" ]; then
runner="primusrun $runner"
else
args="$args $var"
fi
done
$runner $args
NOTE : I had to do this in /usr/lib/jvm/java-7-openjdk-amd64/jre/bin, not in /usr/bin to make it work with Eclipse.
In Eclipse, just add "-3d" in your program arguments and you're good to go !

Categories

Resources