Linux 设置 -Djava.awt.headless=true 后的 Java Headless 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10170609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Headless Exception after setting -Djava.awt.headless=true
提问by mzereba
I am using:
我在用:
Linux Debian 6, Java 1.6.29 and Tomcat 6
Linux Debian 6、Java 1.6.29 和 Tomcat 6
I've seen many posts about it explaining that java.awt
requires X11 libraries..etc., but they are not solving the problem.
我看过很多关于它的帖子,解释说java.awt
需要 X11 库..等,但他们没有解决问题。
I set -Djava.awt.headless=true in my box and I worked around the first problem of the headless environment, now I am getting:
我在我的盒子中设置了 -Djava.awt.headless=true 并且我解决了无头环境的第一个问题,现在我得到:
java.awt.HeadlessException
java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
java.awt.Window.(Window.java:432)
java.awt.Frame.(Frame.java:403)
javax.swing.JFrame.(JFrame.java:202)
org.jfree.ui.ApplicationFrame.(ApplicationFrame.java:65)
...
I know by doing this I just told java that there is no monitor (and in fact running a standalone java program to check if the environment is headless it does return true
). So is it anything to do with the Linux environment like setting the DISPLAY env variable?
我知道这样做我只是告诉 java 没有监视器(实际上运行一个独立的 java 程序来检查环境是否是无头的,它确实返回了true
)。那么是否与设置 DISPLAY env 变量之类的 Linux 环境有关?
I would appreciate your help,
我会很感激你的帮助,
Thanks.
谢谢。
采纳答案by mzereba
Thanks for your reply.
感谢您的回复。
Since you gave me that I've been trying to text it. Still doesn't work in the sense that: is now not throwing that exception anymore, but creating a blank image for some reason.
自从你给我说我一直在试着发短信。从某种意义上说仍然不起作用:现在不再抛出该异常,而是出于某种原因创建了一个空白图像。
So I went from this:
所以我从这个出发:
public BarChartGenerator(org.qcri.power.ws.client.Server oServer, ServerStatistics oServerStat, List lVMs, String path) extends ApplicationFrame {
System.setProperty("java.awt.headless", "true");
boolean headless = GraphicsEnvironment.isHeadless();
System.out.println("Headless: " + headless);
Toolkit tk = Toolkit.getDefaultToolkit();
tk.beep();
// generate dataset
final CategoryDataset dataset = createDataset(lVMs);
setChart(createChart(oServer, oServerStat, lVMs, dataset));
// create PNG of the chart...
setFilename("chart_server"+oServer.getHost()+"_"+System.currentTimeMillis()+".gif");
File fImageFile = new File(path+filename);
try {
ChartUtilities.saveChartAsPNG(fImageFile, chart, PowerInterface.CHART_WIDTH, PowerInterface.CHART_HEIGHT);
} catch (Exception e) {
e.printStackTrace();
}
}
to the following (getting rid of extends ApplicationFrame
) and use the example you gave me:
到以下(摆脱extends ApplicationFrame
)并使用你给我的例子:
public BarChartGenerator(org.qcri.power.ws.client.Server oServer, ServerStatistics oServerStat, List lVMs, String path) {
System.setProperty("java.awt.headless", "true");
boolean headless = GraphicsEnvironment.isHeadless();
System.out.println("Headless: " + headless);
Toolkit tk = Toolkit.getDefaultToolkit();
tk.beep();
// generate dataset
final CategoryDataset dataset = createDataset(lVMs);
setChart(createChart(oServer, oServerStat, lVMs, dataset));
// create PNG of the chart...
setFilename("chart_server"+oServer.getHost()+"_"+System.currentTimeMillis()+".gif");
try {
BufferedImage bufferedImage = getChart().createBufferedImage(PowerInterface.CHART_WIDTH, PowerInterface.CHART_HEIGHT);
ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename()));
} catch (Exception e) {
e.printStackTrace();
}
}
in the log it complains about ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename()));
giving this error (while the file is there!):
在日志中它抱怨ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename()));
给出这个错误(当文件在那里时!):
javax.imageio.IIOException: Can't create output stream!
at javax.imageio.ImageIO.write(ImageIO.java:1560)
at org.qcri.power.util.BarChartGenerator.<init>(BarChartGenerator.java:106)
Any clue?
有什么线索吗?
回答by jalopaba
As you can read in http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/, many components are affected if a display device, keyboard or mouse is not supported. Methods related to Canvas, Panel, and Image components do not need to throw a HeadlessExceptionbecause these components can be given empty peers and treated as lightweight components.
正如您在http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/ 中所读到的,如果不支持显示设备、键盘或鼠标,许多组件都会受到影响。与Canvas、Panel 和 Image 组件相关的方法不需要抛出 HeadlessException,因为这些组件可以被赋予空对等体并被视为轻量级组件。
So the JFrame is the component throwing the HeadlessException.
所以 JFrame 是抛出 HeadlessException 的组件。
If you want to generate charts with jfreechart in a headless environment, this link may help you: http://javaevangelist.blogspot.com.es/2010/11/creating-charts-on-headless-systems.html.
如果您想在无头环境中使用 jfreechart 生成图表,此链接可能对您有所帮助:http: //javaevangelist.blogspot.com.es/2010/11/creating-charts-on-headless-systems.html。
回答by trashgod
You may need to install a VNC server (or something similar), as discussed in this forum thread.
您可能需要安装 VNC 服务器(或类似的东西),如本论坛主题中所述。
Addendum: Instead of saving the chart image as a file, write to the server's output stream using one of the writeChartAsPNG()
methods, as suggested here.
附录:不是将图表图像保存为文件,而是使用其中一种writeChartAsPNG()
方法写入服务器的输出流,如建议here。
回答by Makky
-Djava.awt.headless=false
add above it works a treat :)
在上面添加它是一种享受:)
回答by u1897915
Take off any extends from ApplicationFramewill solve this problem. In most Tomcat deployment environments ,we usually has no X11 Windows package. we needn't a ApplicationFrame (JFram)which will use old AWT objects.
从ApplicationFrame 中去掉任何扩展 将解决这个问题。在大多数 Tomcat 部署环境中,我们通常没有 X11 Windows 包。我们不需要使用旧 AWT 对象的ApplicationFrame (JFram)。
My example
我的例子
BloodChart(String title) extends ApplicationFrame
changes to ->
更改为 ->
BloodChart(String title)
It's work for me , good luck!
这对我有用,祝你好运!