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

标签 java macos awt lwjgl

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

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

努力实现

我也希望能够在我的 MacBook 上生成这些 JFrame。

问题

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

编辑:在我的 Macbook 上的虚拟机中运行它使我能够正确运行该项目。这远非理想的解决方案。我仍在寻找解决这个晦涩问题的方法。

我尝试了什么

  • 未使用 VM 选项运行:previous post 中描述的问题发生。因此这不是一个可行的选择
  • 使用 VM 选项运行:这会在创建 JFrame 时抛出 -Djava.awt.headless

最佳答案

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

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

请参阅此答案底部的我的解决方案和建议以获取快速摘要,还可以在此处查看其他有相同问题的人:Java Creating Instance of BufferedImage Freezes Program


关于为什么您的代码在 Windows 而不是 Mac 上运行的快速说明:这是因为这两个操作系统通常运行不同的 openGL 实现,并且通常 Mac 可能会落后并错过一堆可能解决问题的更新/更改,例如这样它就不会在 openGL 线程上初始化 BufferedImage 时卡住。


如果以上方法不起作用,那么让我们先看看什么是 headless 模式。 (强调我的):

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.

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

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

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

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

这意味着您应该有一段特殊的 headless 代码来处理 headless 图像,然后将图像传回带头的普通 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.

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

我推荐:

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


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

关于java - 制作 JFrame 时抛出 LWJGL java.awt.HeadlessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47006058/

相关文章:

python - os X 上的 virtualenv 错误

java - 如何将 Ellipse2D 和 Rectangle 作为一个形状表示/使用?

java - 改变方向 Android 设备

macos - as3 scanHardware()函数使我的应用程序崩溃

java - 为什么在表格单元格中显示路径而不是图标

objective-c - 不提示解压

java - 为什么JComboBox会忽略空值状态?

java - JMenu 显示在 Label 后面

java - 如何计算Web服务客户端空闲时间

java - 如何用 Java 写入存储在在线服务器上的 .txt 文件?