我在用:
Linux Debian 6,Java 1.6.29和Tomcat 6
我看过很多关于它的文章,它们解释了java.awt需要X11库..etc。的问题,但是他们并没有解决问题。
java.awt
我在框中设置-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) ...
我知道通过这样做,我只是告诉Java没有监视器(实际上,运行一个独立的Java程序来检查环境是否无头,它确实会返回true)。那么这与Linux环境有什么关系,例如设置DISPLAY env变量?
true
多谢您的协助,
谢谢。
感谢您的回复。
自从您告诉我以来,我一直在尝试发短信。从某种意义上说,它仍然不起作用:现在不再抛出该异常,而是出于某种原因创建了空白图像。
所以我从这里出发:
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(); } }
以下内容(摆脱extends ApplicationFrame),并使用您给我的示例:
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(); } }
在日志中,它抱怨ImageIO.write(bufferedImage, "gif", new FileOutputStream(path+getFilename()));给出此错误(文件在那里!):
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)
有什么线索吗?