简体   繁体   English

使用 MAC OS 的 java.awt.Robot 类中的无头环境错误

[英]Headless environment error in java.awt.Robot class with MAC OS

I am trying to capture screen shots in my JavaFX application using Robot class,我正在尝试使用Robot类在我的 JavaFX 应用程序中捕获屏幕截图,

this is the code which I used in my application:这是我在我的应用程序中使用的代码:

Rectangle screenBounds = new Rectangle(Screen.getPrimary().getBounds().getWidth(),
           Screen.getPrimary().getBounds().getHeight());

Robot robot = new Robot();

BufferedImage img = robot.createScreenCapture(new java.awt.Rectangle(
     (int) screenBounds.getX(), (int) screenBounds.getY(), (int) 
             screenBounds.getWidth(), (int) screenBounds.getHeight()));

It is working perfectly in windows operating system , but showing an error of headless environment in MAC OS at Robot robot = new Robot();它在windows 操作系统中运行良好,但在Robot robot = new Robot();处显示MAC OSheadless environment的错误Robot robot = new Robot();

This is to answer my own question, after searching many resources. 这是在搜索了许多资源后回答我自己的问题。

I have used following code to disable headless environment, and the problem is solved. 我使用以下代码来禁用无头环境,问题解决了。

static {

        System.setProperty("java.awt.headless", "false");
}

Thanks. 谢谢。

From their API I can see the following: 从他们的API我可以看到以下内容:

  1. The constructors of Applet and all heavyweight components (*) are changed to throw HeadlessException if a display, keyboard, and mouse are not supported by the toolkit implementation 如果工具包实现不支持显示,键盘和鼠标,则Applet和所有重量级组件(*)的构造函数将更改为抛出HeadlessException
  2. The Robot constructor throws an AWTException if a display, keyboard, and mouse are not supported by the toolkit implementation 如果工具包实现不支持显示,键盘和鼠标,则Robot构造函数会抛出AWTException
  3. Many of the methods in Toolkit and GraphicsEnvironment, with the exception of fonts, imaging, and printing, are changed to throw HeadlessException if a display, keyboard, and mouse are not supported 如果不支持显示,键盘和鼠标,Toolkit和GraphicsEnvironment中的许多方法(字体,图像和打印除外)都会更改为抛出HeadlessException
  4. Other methods that may be affected by lack of display, keyboard, or mouse support, are changed to throw HeadlessException 可能受缺少显示,键盘或鼠标支持影响的其他方法被更改为抛出HeadlessException
  5. It should be worth noting that the HeadlessException is thrown if and only if isHeadless returns true, and that all javadoc comments should specify this 值得注意的是,当且仅当isHeadless返回true并且所有javadoc注释都应指定此时,抛出HeadlessException

So you need to check your hardware and their drivers. 因此,您需要检查硬件及其驱动程序。

You can simply add this line of code System.setProperty("java.awt.headless", "false");您可以简单地添加这行代码System.setProperty("java.awt.headless", "false"); before the Robot robot = new Robot();Robot robot = new Robot(); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM