0

Motive of adding AWT: To add a dialog box in front end to allow users to save file at desired location .

Code is working fine when I am running in local (with Tomcat7.0 in eclipse) and failing when running on Virtual Server with Tomact 7.0 JDK 1.7 and throwing awt headless exception.

No X11 DISPLAY variable was set, but this program performed an operation which is required

Tried the below fix but still not working

System.setProperty(“java.awt.headless”, “false”); 
System.out.println(java.awt.GraphicsEnvironment.isHeadless()); 

Please help how this can be resolved in Java .. In Unix server X11forwarding is enabled

Option 1: Tried headless value as true and getting the same Headless exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.java.Sample#0' defined in ServletContext resource [/WEB-INF/manage-servlet.xml]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.java.Sample]: Constructor threw exception; nested exception is java.awt.HeadlessException:

Option 2: Added the headless value as false and got below exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.java.Sample#0' defined in ServletContext resource [/WEB-INF/manage-servlet.xml]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.java.Sample]: Constructor threw exception; nested exception is java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable. at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)

Note: here changed display variable to DISPLAY:0 but still failed

Option 3: Added display variable as false and added the command false in startup script also then also it failed with below exception

java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment java.lang.Class.forName0(Native Method) java.lang.Class.forName(Class.java:195) java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:102) java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81) java.awt.Window.initGC(Window.java:467)

Swing Code in java file:

System.setProperty("java.awt.headless", "false");
JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Users"); int userSelection = fileChooser.showSaveDialog(this); if (userSelection == JFileChooser.APPROVE_OPTION) { File fileToSave = fileChooser.getSelectedFile(); FileOutputStream fileout = new FileOutputStream(fileToSave.getAbsolutePath()); AUDIT.info("Save as file: " + fileToSave.getAbsolutePath());

I am fed up with this headless exception handling and not getting a way to solve this.

1
  • If it's not possible to resolve this ..plz suggest how to create save dialog box in JSP by accessing a variable in java .
    – mpsimham
    Jan 1, 2021 at 18:49

1 Answer 1

0

Same error I also got when Java UI being loaded. Below command fixed the issue on my CentOS 7.x version.

export DISPLAY=:0

2
  • Tnq fr suggesting but it's not working for me 😔
    – mpsimham
    Jan 2, 2021 at 3:58
  • I am adding export in serenv.sh file .... Display needs to be set in any other file?
    – mpsimham
    Jan 2, 2021 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.