简体   繁体   English

Java 中的 X11 异常

[英]X11 exception in Java

Motive of adding AWT: To add a dialog box in front end to allow users to save file at desired location.添加AWT的动机:在前端添加一个对话框,允许用户将文件保存在所需的位置。

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.当我在本地运行时(在 Eclipse 中使用 Tomcat7.0),代码运行良好,而在使用 Tomact 7.0 JDK 1.7 的虚拟服务器上运行时失败,并抛出 awt 无头异常。

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请帮助如何在 Java 中解决此问题。在 Unix 服务器中启用了 X11forwarding

Option 1 : Tried headless value as true and getting the same Headless exception选项 1 :尝试将 headless 值设为 true 并获得相同的 Headless 异常

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选项 2 :将无头值添加为 false 并获得以下异常

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注意:此处将显示变量更改为DISPLAY:0但仍然失败

Option 3 : Added display variable as false and added the command false in startup script also then also it failed with below exception选项 3 :将显示变量添加为 false 并在启动脚本中添加了命令 false ,然后它也因以下异常而失败

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: java 文件中的 Swing 代码:

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.我已经厌倦了这种无头异常处理并且没有办法解决这个问题。

Same error I also got when Java UI being loaded.加载 Java UI 时,我也遇到了同样的错误。 Below command fixed the issue on my CentOS 7.x version.下面的命令修复了我的 CentOS 7.x 版本的问题。

export DISPLAY=:0出口显示=:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM