簡體   English   中英

Java 中的 X11 異常

[英]X11 exception in Java

添加AWT的動機:在前端添加一個對話框,允許用戶將文件保存在所需的位置。

當我在本地運行時(在 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

嘗試了以下修復但仍然無法正常工作

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 :將無頭值添加為 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM