reproduce exception

[[email protected] ~]$ java -version
java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03, mixed mode)

[[email protected] ~]$ pwd
/apps/test/

[[email protected] ~]$ ls -l
total 40
drwxrwxr-x. 8 db db 4096 Jul 24 16:21 bin
drwxrwxr-x. 6 db db 4096 Jul 24 16:21 file
-rw-rw-r--. 1 db db 1105 Jul 24 16:21 index.htm
-rw-rw-r--. 1 db db  836 Jul 24 16:21 index.jsp
drwxrwxr-x. 2 db db 4096 Jul 24 16:21 jsp
drwxrwxr-x. 2 db db 4096 Jul 24 16:21 logs
drwxrwxr-x. 2 db db 4096 Jul 24 16:20 META-INF
-rw-r--r--. 1 db db 4508 Jul 25 15:28 ReportTest.java
drwxrwxr-x. 7 db db 4096 Jul 25 15:36 WEB-INF

exception

[[email protected] ~]$ javac ReportTest.java 
[[email protected] ~]$ java ReportTest
... ...
3154 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
    at net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:83)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:174)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:74)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:56)
    at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:143)
    at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:53)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
    at ReportTest.main(ReportTest.java:77)
    
    
[[email protected] ~]$ java  -Djava.awt.headless=true ReportTest
... ...
3154 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
[[email protected]

set profile and restart tomcat

[[email protected] ~]$ cat ~/.bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs
export CATALINA_HOME=/apps/test/tomcat
PATH=$PATH:$HOME/bin:$CATALINA_HOME/bin

export PATH

alias java='java -Djava.awt.headless=true'
unset DISPLAY

[[email protected] ~]$ ps -ef | grep tomcat
db     4003     1  1 14:53 ?        00:00:46 /usr/java/jdk1.6.0_33/bin/java -Djava.util.logging.config.file=/apps/test/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/apps/test/tomcat/endorsed -classpath /apps/test/tomcat/bin/bootstrap.jar -Dcatalina.base=/apps/db/tomcat -Dcatalina.home=/apps/db/tomcat -Djava.io.tmpdir=/apps/test/tomcat/temp org.apache.catalina.startup.Bootstrap start
db    13305  6465  0 15:56 pts/3    00:00:00 grep tomcat
[[email protected] ~]$ kill -9 4003
[[email protected] ~]$ catalina.sh start
Using CATALINA_BASE:   /apps/db/tomcat
Using CATALINA_HOME:   /apps/db/tomcat
Using CATALINA_TMPDIR: /apps/test/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.6.0_33
Using CLASSPATH:       /apps/test/tomcat/bin/bootstrap.jar

awt test

source code

[[email protected] sofware]# cat GETest.java 
import java.awt.GraphicsEnvironment;

public class GETest{

    public static void main(String[] args){

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        System.out.println("class: " + ge.getClass());
        System.out.println("isHeadless: " + ge.isHeadless());
    }

}

compile and run test

[[email protected] sofware]# javac GETest.java 
[[email protected] sofware]# java GETest
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
    at GETest.main(GETest.java:7)

[[email protected] sofware]# java -Djava.awt.headless=true GETest
class: class sun.java2d.HeadlessGraphicsEnvironment
isHeadless: true
[[email protected] sofware]# echo $DISPLAY
localhost:12.0
[[email protected] sofware]# java GETest
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using 'localhost:12.0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:131)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
    at GETest.main(GETest.java:7)

unset DISPLAY

[[email protected] sofware]# unset DISPLAY
[[email protected] sofware]# java GETest
class: class sun.java2d.HeadlessGraphicsEnvironment
isHeadless: true

Summary

1) Java Technology Community

How to resolve java Exceptions ? :java.awt.HeadlessException

2) java.awt.HeadlessException

I have seen this Exception being encountered by many applications, which use UI (AWT/Swing) APIs .

3) When is this exception thrown ?

java.awt.HeadlessException is thrown on a machine which is headless when -Djava.awt.headless commandline option is set to true and Heavyweight AWT components like Applet, Button, Checkbox, Choice, FileDialog, Label, List, Menu, MenuBar, MenuComponent, MenuItem, PopupMenu, Scrollbar,ScrollPane, TextArea, TextComponent, Frame, Window, Dialog, JApplet, JFrame, JWindow, JDialog and TextField are used in the environment.

4) What is the option -Djava.awt.headless all about?

Many environments, such as mainframe machines and dedicated servers, do not support a display, keyboard, or mouse. On Such machines if we try to to use AWT package,we will get an error like this “Can’t connect to X11 window server using ‘:0.0’ as the value of the DISPLAY variable. at sun.awt.X11GraphicsEnvironment.initDisplay” This error is thrown as the AWT package is causing the X11 server to open the DISPLAY. To correct this problem, We need to tell the Java AWT package that the X11 server is headless (has no display). To run our environment with a headless implementation, the follow property may be specified at the java command line:

-Djava.awt.headless=true

This will tell the AWT package about the X11 server being headless. This option is available from Java 1.4 onwards.

5) Why we get a “HeadlessException” after setting this option ?

Headless support is enabled by the GraphicsEnvironment methods “isHeadless” and “isHeadlessInstance”.

These methods check whether X11 server has a DISPLAY or its headless(without a DISPLAY).

The heavyweight AWT components like Applet, Button, Checkbox, Choice, FileDialog, Label, List, Menu, MenuBar, MenuComponent, MenuItem, PopupMenu, Scrollbar, ScrollPane, TextArea, TextComponent, Frame, Window, Dialog, JApplet, JFrame, JWindow, JDialog and TextField are affected by the lack of DISPLAY,keyboard and mouse etc. Therefore ,if used in a “headless” environment they all will throw a “HeadlessException” like this -

java.awt.HeadlessException 
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:150) 
at java.awt.Window.<init>(Window.java:311) 
at java.awt.Frame.<init>(Frame.java:431) 
at java.awt.Frame.<init>(Frame.java:396)

6) How can we resolve this exception/related erros ?

-Check whether the X11 server is installed -Check whether DISPLAY variable is properly set -Heavyweight awt/swing components which requires display, mouse keyboard should be run on client side rather than server side.



blog comments powered by Disqus

Published

25 July 2013

Categories

Tags

Github