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.
			// This is where the application is still doing stuff. I have closed the gui, and want to log off now.
I discussed this on and you can find the link to the thread below. It also contains additional background information on my question.

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(
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(
        at sun.awt.X11.XToolkit.<clinit>(
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(
        at java.awt.Toolkit$
        at Method)
        at java.awt.Toolkit.getDefaultToolkit(
        at sun.swing.SwingUtilities2$AATextInfo.getAATextInfo(
        at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(
        at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(
        at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(
        at javax.swing.UIManager.setLookAndFeel(
        at javax.swing.UIManager.setLookAndFeel(
        at javax.swing.UIManager.initializeDefaultLAF(
        at javax.swing.UIManager.initialize(
        at javax.swing.UIManager.maybeInitialize(
        at javax.swing.UIManager.getUI(
        at javax.swing.JPanel.updateUI(
        at javax.swing.JPanel.<init>(
        at javax.swing.JPanel.<init>(
        at javax.swing.JPanel.<init>(
        at Main.main(
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