x11异常

xghobddn  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(357)

添加awt的动机:在前端添加一个对话框,允许用户将文件保存在所需的位置。
当我在本地运行(在eclipse中使用tomcat7.0)时,代码运行正常;当在虚拟服务器上使用tomact7.0jdk1.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:尝试将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:

选项2:将headless值添加为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:添加display变量为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());

我受够了这种无头异常处理,没有办法解决这个问题。

emeijp43

emeijp431#

同样的错误,我也得到了当JavaUI被加载。下面的命令修复了我的centos 7.x版本的问题。
导出显示=:0

相关问题