如何解决Java 中的 X11 异常
添加AWT的动机:在前端添加一个对话框,允许用户将文件保存在所需的位置。
当我在本地运行(在 Eclipse 中使用 Tomcat7.0)时代码工作正常,而在使用 Tomact 7.0 JDK 1.7 的虚拟服务器上运行时失败并抛出 awt headless 异常。
No X11 DISPLAY variable was set,but this program performed an operation which is required
尝试了以下修复,但仍然无效
System.setProperty(“java.awt.headless”,“false”);
System.out.println(java.awt.GraphicsEnvironment.isHeadless());
请帮助如何在 Java 中解决此问题.. 在 Unix 服务器中 X11forwarding 已启用
选项 1:尝试将无头值设为 true 并获得相同的无头异常
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:
选项 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)
注意:此处将显示变量更改为 DISPLAY:0
但仍然失败
选项 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)
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());
我厌倦了这种无头异常处理,却没有办法解决这个问题。
解决方法
我在加载 Java UI 时也遇到了同样的错误。下面的命令修复了我的 CentOS 7.x 版本的问题。
导出显示=:0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。