设置java.awt.headless =真正的编程 [英] Setting java.awt.headless=true programmatically

查看:1756
本文介绍了设置java.awt.headless =真正的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置 java.awt.headless = TRUE 应用程序启动的过程中,但它看起来像我来不及和非无头模式已经启动:

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 is already started:

static {
    System.setProperty("java.awt.headless", "true");
    /* java.awt.GraphicsEnvironment.isHeadless() returns false */
}

有没有无头设置为true,另一种方式 -Djava.awt.headless =旁边真?我想preFER不配置控制台上的任何东西。

Is there another way set headless to true beside -Djava.awt.headless=true? I would prefer to not configure anything on the console.

推荐答案

我正在同一个的main(),其中静态​​加载常量的JFreeChart的不同部分的类(和其他静态code)。

I was working with a main() in a class which statically loads different parts of JFreeChart in Constants (and other static code).

移动静态负载块类的顶级解决了我的问题。

Moving the static loading block to the top of the class solved my problem.

这不工作:

  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() {}
  }

对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!

这篇关于设置java.awt.headless =真正的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆