jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question What causes the error "Can't connect to X11 window server"?
Derived from A question posed by Sann Maung
Topics Java:API:AWT:Graphics, Platform:UNIX:Linux
Author Nathan Meyers PREMIUM
Created Oct 4, 2000 Modified Oct 11, 2001


Answer
The Sun AWT classes on Unix and Linux have a dependence on the X Window System: when you use the classes, they expect to load X client libraries and be able to talk to an X display server. This makes sense if your client has a GUI... unfortunately, it's required even if your client uses AWT but does not have a GUI. For example, you need access to an X server to use the java.awt.BufferedImage class.

Access to an X display server means a few things:

  • An X display server is running somewhere.
  • The environment in which you run Java includes an environment variable DISPLAY identifying how to reach the server.
  • There are no security settings in the server to prevent your client from opening a connection.

In general, if you're running your program from a terminal within the X Window System, all these things are true and the program just works. If you can run other X applications, like xterm or xclock, you should be able to run your Java AWT application.

In non-graphical environments, such as a servlet engine, your program may not know how to find or connect to an X display server. A common solution here is to run a special non-display version of the X display server, Xvfb, and set DISPLAY to point to it.

New information for JDK1.4: JDK1.4 evidently includes a new property that will allow the AWT to run in a headless (without a display) environment. This setting is supposed to solve the problem: java.awt.headless=true



Is this item helpful?  yes  no     Previous votes   Yes: 4  No: 0



Comments and alternative answers

Comment on this FAQ entry

See also
Alex Chaffee PREMIUM, Jul 2, 2001
See also Is there any way to generate offscreen images with... for more info.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Java 1.4 headless - not quite there.
Mike Bremford, Mar 14, 2002  [replies:2]

Unfortunately we've just been testing the headless support with the Java 1.4.0 release on Linux, and it's not quite what we expected.

It's possible to run many AWT methods now without requiring a running X server, although an equal number will throw a java.awt.HeadlessException. In particular, the oft-quoted method for creating a server side image:

  Frame f = new Frame();
  f.addNotify();
  Image i = f.createImage(width,height);

will throw an Exception when running headless. You can easily replace this with:

  Image i = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);

which gives identical results however.

So far so good. What's not obvious though is that even though Java 1.4 doesn't need an X server to connect to, it still needs X11 installed. The headless toolkit still makes calls to a native library (jre/lib/i386/libawt.so), which requires a number of libraries which are part of the X11 distribution. For those running RedHat 7.1 or later, these libraries are all in the XFree86-devel package.

Almost there, Sun, but not quite...



Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 0



Reply to this answer/comment  Help  
Re: Java 1.4 headless - not quite there.
Guilherme Silva, Mar 2, 2004  [replies:1]
Hi all,
Unfortunately, setting this property using this command:
System.setProperty("java.awt.headless", "true");

inside a JSP didn't solve the problem. It only works in a console java class.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re[2]: Java 1.4 headless - not quite there.
Isidoro Legido Martínez, Sep 6, 2004
Just defines de envirnoment variable in your servlet engine. For example if you are using Tomcat, modifies catalina.sh adding the line 

  export CATALINA_OPTS=-Djava.awt.headless=trued

I hope it helps.

Isi,


Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 0



Reply to this answer/comment  Help  
Must run as root (sudo)
Joseph Schmoley, Aug 12, 2008

I'm running an X11 system on Centos 5.2 final (2.6.18-92.1.6.el5), logged in via X, can run xclock and other X GUI apps no problem, but I still experienced this error message.

I'm not sure if this is the actual fix but it worked for me... my user account (not root) on this machine has sudo privileges. If I issue the same command with sudo, the applet window appears without a problem.

Hope this helped someone.

-Jac

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  



Ask A Question



 
Related Links

AWT FAQ

AWT Forum

Sun's AWT Home Page

The Java Tutorial (Good AWT & Swing Coverage)

Effective Layout Management

Layout Manager Launch

Creating a Custom Layout Manager

Drag and Drop resources

Linux FAQ

Linux Forum

The Blackdown Organization - porting Java for multiple Linux platforms

Sun's JDK1.2.2 for Linux

IBM's JDK1.3 for Linux

Kaffe - a cleanroom Java implementation

gcj - the Gnu Compiler for Java

Java Programming on Linux (book by FAQ manager)

Wish List
Features
About jGuru
Contact Us

 


Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers