java.awt.headless 模式(Linux, java.awt.headless and the DISPLAY environment variable)

92 篇文章 0 订阅

http://blog.chinaunix.net/uid-25098298-id-287544.html

1. 什么是 java.awt.headless?
Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标。

2. 何时使用和headless mode?
Headless模式虽然不是我们愿意见到的,但事实上我们却常常需要在该模式下工作,尤其是服务器端程序开发者。因为服务器(如提供Web服务的主机)往往可能缺少前述设备,但又需要使用他们提供的功能,生成相应的数据,以提供给客户端(如浏览器所在的配有相关的显示设备、键盘和鼠标的主机)。

3. 如何使用和Headless mode?
一般是在程序开始激活headless模式,告诉程序,现在你要工作在Headless mode下,就不要指望硬件帮忙了,你得自力更生,依靠系统的计算能力模拟出这些特性来:
System.setProperty("java.awt.headless", "true");

4. 测试程序:

  1. import java.awt.Graphics;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import javax.imageio.ImageIO;

  5. public class TestCHSGraphic{
  6.     
  7.     public static void main(String[] args) throws Exception{
  8.         //设置Headless模式
  9.         System.setProperty("java.awt.headless", "true");
  10.         BufferedImage bi = new BufferedImage(200, 100, BufferedImage.TYPE_INT_RGB);
  11.         Graphics g = bi.getGraphics();
  12.         String s = "Headless模式测试";
  13.         g.drawString(new String(s.getBytes(), "GB2312"), 50, 50);
  14.         ImageIO.write(bi, "jpeg", new File("test.jpg"));
  15.     }
  16.     
  17. }
JDK1.4以上版本编译运行会在相同目录下生成一幅图片,如下:


5. Toolkit类的getDefaultToolkit函数api中文说明
public static Toolkit getDefaultToolkit()获取默认工具包。如果名为 "java.awt.headless" 的系统属性被设置为 true,则使用 Toolkit 的 headless 实现。如果不存在 "java.awt.headless" 或 "java.awt.headless" 被设置为 false,且存在名为 "awt.toolkit" 的系统属性,则该属性将被视为 Toolkit 子类的名称;否则将使用特定于平台的默认 Toolkit 实现。还可以使用 Sun 引用实现中指定的属性 'assistive_technologies' 将其他类加载到 VM 中,该属性是在 'accessibility.properties' 文件的一个行中指定的。形式是 "assistive_technologies=...",其中 "..." 是以逗号分隔的、要加载的辅助技术类的列表。每个类都以给定的顺序加载,并且要使用 Class.forName(class).newInstance() 创建每个类的单独实例。此操作在创建 AWT 工具包之后进行。所有错误都通过 AWTError 异常来处理。 返回: 默认工具包。 抛出: AWTError - 如果不能找到工具包,或者不能访问或实例化工具包。

参考: http://hi.baidu.com/lovemywolf/blog/item/be08ebed2293734979f05595.html
   jdk api 文档

http://leshazlewood.com/2009/08/26/linux-javaawtheadless-and-the-display-environment-variable/

Just a quick note to self:

After looking at the JDK source code today, we found that even when setting -Djava.awt.headless=true, if a script executes with the DISPLAY environment variable set, the AWT graphics environment will still use a GraphicsEnvironment representing that DISPLAY.

A major problem with this is if the shell that launched the script that started the java process ever executes, the GraphicsEnvironment reflecting the DISPLAY will be removed and any Object instances reflecting that environment would be invalidated or Classes once available will no longer be available. resulting in a NoClassDefFoundException.

Bottom line: on Linux, if setting -Djava.awt.headless=true in a script, ensure that you unset DISPLAY in the script before starting the java process. It will save some headaches in debugging.

 

==============https://confluence.atlassian.com/plugins/viewsource/viewpagesrc.action?pageId=133693889

When attempting to use a Chart either with the Chart Plugin (JIRA 3.13) or the Pie Chart Gadget (4.0+), no output is rendered. Errors like the following appear in the logs:

2008-02-04 12:45:36,051 TP-Processor6 ERROR [500ErrorPage] Exception caught in 500 page /usr/local/java/jdk1.6.0_02/jre/lib/i386/xawt/libmawt.so: libXtst.so.6: cannot open shared object file: No such file or directory
java.lang.UnsatisfiedLinkError: /usr/local/java/jdk1.6.0_02/jre/lib/i386/xawt/libmawt.so: libXtst.so.6: cannot open shared object file: No such file or directory
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1647)
	at java.lang.Runtime.load0(Runtime.java:770)
	at java.lang.System.load(System.java:1005)
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1668)
	at java.lang.Runtime.loadLibrary0(Runtime.java:823)
	at java.lang.System.loadLibrary(System.java:1030)
	at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:50)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.awt.Toolkit.loadLibraries(Toolkit.java:1594)
	at java.awt.Toolkit.<clinit>(Toolkit.java:1616)
	at java.awt.Color.<clinit>(Color.java:263)
	at com.atlassian.jira.ext.charting.jfreechart.ChartUtils.<clinit>(ChartUtils.java:45)
	at com.atlassian.jira.ext.charting.data.DataCollector.populatePieChart(DataCollector.java:669)
	at com.atlassian.jira.ext.charting.charts.singlefieldpie.SingleFieldPieChart.getViewHtml(SingleFieldPieChart.java:57)
	at com.atlassian.jira.ext.charting.charts.PortletSearchView.writeSearchResults(PortletSearchView.java:89)

In addition, errors like the following appear:

2008-02-04 12:49:14,654 TP-Processor2 ERROR [webwork.dispatcher.ServletDispatcher] Could not execute action
java.lang.NoClassDefFoundError: Could not initialize class com.atlassian.jira.ext.charting.jfreechart.ChartUtils
at com.atlassian.jira.ext.charting.data.DataCollector.retrieveOrMakeSearchRequest(DataCollector.java:205)

Cause

Charts that create custom graphics require X11 libraries and an X11 session to assist in creating their output.

Resolution

  1. Install the required libXtst-devel and libXrender-devel RPM to the server per the instructions on our Confluence UNIX and X11 Dependencids page.
  2. Check your DISPLAY environment variable and set the -Djava.awt.headless=true property in your JAVA_OPTS environment variable

Related Content

{dynamiccontentbylabel:showLabels=false|showSpace=false}


{kbsurvey}

{htmlcomment}
Regular Expression: java.lang.NoClassDefFoundError: Could not initialize class com.atlassian.jira.ext.charting.jfreechart.ChartUtils
http://support.atlassian.com/browse/JSP-19704
{htmlcomment}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值