制作 JFrame 时抛出 LWJGL java.awt.HeadlessException

LWJGL java.awt.HeadlessException thrown when making a JFrame

您好,我正在处理一个小组项目,代码可以在我队友的 PC 上运行,但我一直遇到 MacOS 特定错误。而这一次我似乎被卡住了(没有简单的谷歌搜索答案)。

中,我发现我需要“-Djava.awt.headless=true”作为 VM 设置才能正确地 运行 我的模拟。现在,当我尝试在某些 JFrame 中生成时,由于该 VM 标志,它们都遇到了可爱的“java.awt.HeadlessException”异常。

正在努力实现

我也希望能够在我的 Mac书中生成这些 JFrame。

问题

我需要 -Djava.awt.headless 同时为真和假,这样我的程序才能在 Mac 上正确地 运行。如果我正确地理解了我的问题,那意味着我手上有一个大问题。

编辑:运行在我的 Mac 书上的 VM 中安装它允许我正确地 运行 项目。这远非理想的解决方案。我仍在寻找解决这个晦涩问题的方法。

我试过的

解决此问题的最佳方法可能是返回并以不同的方式解决您原来的问题。

您必须确保您没有在主线程(GLFW 线程)中初始化 BufferedImage,它 必须 单独完成。很难从您最初的问题中分辨出来,但这看起来像是那里的部分原因。启动一个新线程来代替图像处理。

在这个答案的底部查看我的解决方案和建议以获得快速总结,也可以在此处查看其他有相同问题的人:


快速说明为什么您的代码适用于 Windows 而不是 Mac:这是因为 OS 通常 运行 openGL 的不同实现,通常 Mac 可能会落后并错过一堆 updates/changes 可能会解决这样的问题,因此在 openGL 线程上初始化 BufferedImage 时它不会冻结。


如果以上方法都不行,那么让我们先看看什么是无头模式。 (强调我的):

See link at bottom for full article and more info.

Headless mode is a system configuration in which the display device, keyboard, or mouse is lacking. Sounds unexpected, but actually you can perform different operations in this mode, even with graphic data.

Where it is applicable? Let's say that your application repeatedly generates a certain image, for example, a graphical authorization code that must be changed every time a user logs in to the system. When creating an image, your application needs neither the display nor the keyboard. Let's assume now that you have a mainframe or dedicated server on your project that has no display device, keyboard, or mouse. The ideal decision is to use this environment's substantial computing power for the visual as well as the nonvisual features. An image that was generated in the headless mode system then can be passed to the headful system for further rendering.

那么什么时候应该使用无头模式:

On a machine that has no display device, keyboard, or mouse.

那不是你吗?但是,如果那是你(LWJGL?),那么让我们看看如何使用无头模式:

An image that was generated in the headless mode system then can be passed to the headful system for further rendering.

这意味着你应该有一段特殊的无头代码来处理你的无头图像,然后将图像传回有头的普通 JFrame。

那么为什么它对你来说失败了:

Many components are affected if a display device, keyboard, or mouse is not supported. An appropriate class constructor throws a HeadlessException

  • Button
  • Checkbox
  • Choice
  • Dialog
  • FileDialog
  • Frame
  • Label
  • List
  • Menu
  • MenuBar
  • MenuItem
  • PopupMenu
  • Scrollbar
  • ScrollPane
  • TextArea
  • TextField
  • Window

问题解决方案:

some classes, such as Canvas or Panel, can be executed in headless mode.

完美,所以我们只需要注意无头模式下使用的内容。您询问了如何既可以使用也可以不使用无头模式,而不是在需要时使用 System.setProperty("java.awt.headless", "true"); 使用 VM 选项 -Djava.awt.headless you can do it programmatically within your code 全局设置无头模式。 JFrame 应该是正常的(不是无头的),但是你可以毫无问题地生成一个 JPanel 作为无头的。

我推荐:

您创建了一个正常的有头主线程,没有生成 JFrames 的 VM 选项,然后使用该主线程生成一个新的子线程并将该线程中的 LWJGL 位设置为无头,这样您就可以 运行 您的 LWJGL 代码没有问题,同时您仍然可以从主线程获得 JFrames。请记住确保缓冲图像未在主线程中完成 LWJGL/OpenGL。


无头信息来源: http://www.oracle.com/technetwork/articles/javase/headless-136834.html