Skip to Main Content

Java SE (Java Platform, Standard Edition)

How to close connection to GraphicsEnvironment / x-server

Joe_2000Feb 18 2013
Hello all.

I have a long-running terminal-application which has a command to bring up a (swing) gui to monitor current execution state. The gui can be closed but the application will keep running.

This application is running on a remote server, and I am running it from my local machine through ssh. (All of this is happening on Linux)

Now, in order to be able to close the ssh connection without having to exit the running application I am using a software called screen that allows me to detach the process from the terminal. I can then logout from the ssh session without killing the process. Unfortunately this only works until opening the GUI, because once I did that the ssh connection cannot be closed anymore without exiting the application.

Note that even closing the GUI (i.e. calling dispose on the JFrame that carries it) does not help. After some trying around I found that as soon as I instantiate any gui components - without even showing them at all - the problem occurs. E.g. the code below reproduces the problem.
import javax.swing.*;

public class Main 
{
	public static void main(String[] args) 
	{
		new JPanel();  // commenting this out stops the issue from reproducing.
		while(true)  
		{
			// This is where the application is still doing stuff. I have closed the gui, and want to log off now.
			Thread.yield();
		}
	}
}
I discussed this on LinuxQuestions.org and you can find the link to the thread below. It also contains additional background information on my question.
http://www.linuxquestions.org/questions/showthread.php?p=4894628#post4894628

Based on this discussion my current assumption is that as soon as any swing components are instantiated the class sun.awt.X11GraphicsEnvironment comes in and starts to make a connection to the X-Window server. The main reason why I believe that it's that particular class is that I am getting the following exception when I try to run the above code without enabling x11 fowarding in the ssh connection.
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
        at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:112)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at java.awt.Toolkit$2.run(Toolkit.java:849)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:841)
        at sun.swing.SwingUtilities2$AATextInfo.getAATextInfo(SwingUtilities2.java:121)
        at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(MetalLookAndFeel.java:1564)
        at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(BasicLookAndFeel.java:147)
        at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(MetalLookAndFeel.java:1599)
        at javax.swing.UIManager.setLookAndFeel(UIManager.java:530)
        at javax.swing.UIManager.setLookAndFeel(UIManager.java:570)
        at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1320)
        at javax.swing.UIManager.initialize(UIManager.java:1407)
        at javax.swing.UIManager.maybeInitialize(UIManager.java:1395)
        at javax.swing.UIManager.getUI(UIManager.java:991)
        at javax.swing.JPanel.updateUI(JPanel.java:126)
        at javax.swing.JPanel.<init>(JPanel.java:86)
        at javax.swing.JPanel.<init>(JPanel.java:109)
        at javax.swing.JPanel.<init>(JPanel.java:117)
        at Main.main(Main.java:7)
This is also why I posted this question in this category, I hope this was appropriate.

I think that in order to be able to close the running ssh connection gracefully (i.e. without killing my application in the process) I have to find a way to close this connection to the X-Window server in the java application.

Could anybody enlighten me how to tell the JVM to cut the connection to the x-server? Or, obviously, let me know if I am on the wrong track :-)
Many thanks in advance either way.
Post Details
Locked on Mar 18 2013
Added on Feb 18 2013
0 comments
1,433 views