以编程方式设置 java.awt.headless=true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2552371/
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
Setting java.awt.headless=true programmatically
提问by reto
I'm trying to set java.awt.headless=true
during the application startup but it appears like I'm too late and the non-headless mode has already started:
我正在尝试java.awt.headless=true
在应用程序启动期间进行设置,但似乎为时已晚并且非无头模式已经启动:
static {
System.setProperty("java.awt.headless", "true");
/* java.awt.GraphicsEnvironment.isHeadless() returns false */
}
Is there another way set headless to true beside -Djava.awt.headless=true
? I would prefer not configure anything on the console.
有没有另一种方法将 headless 设置为 true 旁边-Djava.awt.headless=true
?我不想在控制台上配置任何东西。
采纳答案by reto
I was working with a main()
in a class which statically loads different parts of JFreeChart in Constants (and other static code).
我正在使用一个main()
类中的 a,该类在常量(和其他静态代码)中静态加载 JFreeChart 的不同部分。
Moving the static loading block to the top of the class solved my problem.
将静态加载块移动到类的顶部解决了我的问题。
This doesn't work:
这不起作用:
public class Foo() {
private static final Color COLOR_BACKGROUND = Color.WHITE;
static { /* too late ! */
System.setProperty("java.awt.headless", "true");
System.out.println(java.awt.GraphicsEnvironment.isHeadless());
/* ---> prints false */
}
public static void main() {}
}
Have java execute the static block as early as possible by moving it to the top of the class!
通过将静态块移动到类的顶部,让 java 尽早执行静态块!
public class Foo() {
static { /* works fine! ! */
System.setProperty("java.awt.headless", "true");
System.out.println(java.awt.GraphicsEnvironment.isHeadless());
/* ---> prints true */
}
private static final Color COLOR_BACKGROUND = Color.WHITE;
public static void main() {}
}
When thinking about it this makes perfectly sense :). Juhu!
当考虑它时,这是完全有道理的:)。巨呼!
回答by pitpod
This should work because the call to System.setProperty is before the creation of the toolkit:
这应该有效,因为对 System.setProperty 的调用是在工具包创建之前:
public static void main(String[] args)
{
// Set system property.
// Call this BEFORE the toolkit has been initialized, that is,
// before Toolkit.getDefaultToolkit() has been called.
System.setProperty("java.awt.headless", "true");
// This triggers creation of the toolkit.
// Because java.awt.headless property is set to true, this
// will be an instance of headless toolkit.
Toolkit tk = Toolkit.getDefaultToolkit();
// Check whether the application is
// running in headless mode.
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
System.out.println("Headless mode: " + ge.isHeadless());
}
回答by Bharat Joshi
I think this parameter can be set by passing it as an argument to Java Virtual Machine e.g.
我认为可以通过将它作为参数传递给 Java 虚拟机来设置这个参数,例如
-Djava.awt.headless=true.
Not sure if this would have impact on other components of your application.
-Djava.awt.headless=true.
不确定这是否会影响应用程序的其他组件。
回答by Carl Sayres
Here is a completely different approach.
这是一种完全不同的方法。
try {
Field defaultHeadlessField = java.awt.GraphicsEnvironment.class.getDeclaredField("defaultHeadless");
defaultHeadlessField.setAccessible(true);
defaultHeadlessField.set(null,Boolean.FALSE);
Field headlessField = java.awt.GraphicsEnvironment.class.getDeclaredField("headless");
headlessField.setAccessible(true);
headlessField.set(null,Boolean.TRUE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
I am using this successfully to draw on server-side BufferedImages in a headless environment (Ubuntu). The nice thing about this is that it does not require setting any -D variables on the command line, nor do you need to set the DISPLAY variable.
我正在成功地使用它在无头环境 (Ubuntu) 中绘制服务器端 BufferedImages。这样做的好处是它不需要在命令行上设置任何 -D 变量,也不需要设置 DISPLAY 变量。
You can also execute this code at any time. You don't need to worry about invoking this before other classes are loaded.
您也可以随时执行此代码。您无需担心在加载其他类之前调用它。
I suppose this might not work if you were trying to drive a Swing interface on a remote machine from a headless environment.
我想如果您试图从无头环境中驱动远程机器上的 Swing 接口,这可能不起作用。
回答by Mike Kinney
You can set the JAVA_TOOL_OPTIONS
like this:
你可以这样设置JAVA_TOOL_OPTIONS
:
JAVA_TOOL_OPTIONS: -Djava.awt.headless=true
JAVA_TOOL_OPTIONS: -Djava.awt.headless=true
Then any new processes will use that setting during the session. You can add it to your .bashrc
or .bash_profile
(or whatever shell's startup file) for all subsequent sessions.
然后任何新进程将在会话期间使用该设置。您可以将它添加到您的.bashrc
或.bash_profile
(或任何 shell 的启动文件)中以用于所有后续会话。
You can also add it to the first line of a groovy script like this:
您还可以将其添加到 groovy 脚本的第一行,如下所示:
cat hello.groovy
#!/usr/bin/env groovy -Djava.awt.headless=true
println hello