Linux系统中缺少GUI设备引起的异常

缺少X11显示设置

Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException:   
No X11 DISPLAY variable was set, but this program performed an operation which requires it.  
        at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159) 

解决方案: 在linux的启动文件/etc/profile中增加了 export DISPLAY=localhost:0

缺少X11GraphicsEnvironment

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment  
        at java.lang.Class.forName0(Native Method)  

解决方案: 在java程序启动时候,增加了-DJava.awt.headless=true参数

-DJava.awt.headless=true的作用

对于一个Java服务器来说经常要处理一些图形元素,例如地图的创建或者图形和图表等。
这些API基本上总是需要运行一个X-server以便能使用AWT(Abstract Window Toolkit,抽象窗口工具集)。然而运行一个不必要的X-server并不是一种好的管理方式.
有时你甚至不能运行X-server,因此最好的方案是运行headless服务器,来进行简单的图像处理.
以上转摘自:https://www.cnblogs.com/princessd8251/p/4000016.html

项目中的问题回溯

1.问题发生情况:使用poi导出Excel

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>

2.完整报错信息如下:

Caused by: java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
    at sun.awt.X11FontManager.isHeadless(X11FontManager.java:509)
    at sun.awt.X11FontManager.getFileNameFromPlatformName(X11FontManager.java:189)
    at sun.font.SunFontManager.initCompositeFonts(SunFontManager.java:3481)
    at sun.font.SunFontManager.access$700(SunFontManager.java:65)
    at sun.font.SunFontManager$2.run(SunFontManager.java:545)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.font.SunFontManager.<init>(SunFontManager.java:376)
    at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
    at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
    at java.awt.Font.getFont2D(Font.java:491)
    at java.awt.Font.canDisplayUpTo(Font.java:2060)
    at java.awt.font.TextLayout.singleFont(TextLayout.java:470)
    at java.awt.font.TextLayout.<init>(TextLayout.java:531)
    at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:275)
    at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.<init>(AutoSizeColumnTracker.java:117)
    at org.apache.poi.xssf.streaming.SXSSFSheet.<init>(SXSSFSheet.java:79)
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:656)
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:246)
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:204)
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:179)
    at com.winning.polaris.engine.view.utils.WExcelUtils.createExcelFile(WExcelUtils.java:181)
    at com.winning.polaris.engine.view.utils.WExcelUtils.createRuleCusDataExcle(WExcelUtils.java:165)
    at com.winning.polaris.engine.view.rest.controller.EngineRuleController.knowledgeDownload(EngineRuleController.java:645)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:376)
    ... 37 more

参考链接:
http://www.oschina.net/translate/using-headless-mode-in-java-se
https://www.cnblogs.com/wudi-dudu/p/7871405.html

    原文作者:鱼塘鱼
    原文地址: https://www.jianshu.com/p/2781f6e3b367
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞