macos LWJGL java。awt。制作JFrame时抛出的HeadlessException
嗨,我正在做一个小组项目,代码在我队友的电脑上运行,但我一直遇到MacOS特定的错误。这一次,我似乎被卡住了(没有容易找到的答案)
在a previous post中,我发现我需要“-Djava.awt.headless=true
”作为VM设置来正确运行模拟。现在,当我尝试在一些JFrame中生成时,它们都会遇到一个可爱的“java.awt.HeadlessException
”异常,因为这个VM标志
努力实现
我也希望能够在我的MacBook上生成这些JFrame
问题
我需要-Djava.awt.headless
同时为真和假,我的程序才能在Mac上正常运行。如果我正确理解了我的问题,那就意味着我手上有一个大问题
编辑:在Macbook上的虚拟机上运行它可以让我正确运行项目。这远远不是一个理想的解决方案。我仍在寻找解决这个模糊问题的办法
我试过的
- 未使用VM选项运行:出现previous post中描述的问题。因此,这不是一个可行的选择
- 使用VM选项运行:创建JFrame时会抛出
-Djava.awt.headless
李>
# 1 楼答案
解决这个问题的最佳方法可能是回到过去,以不同的方式解决最初的问题
您必须确保没有在主线程(GLFW线程)中初始化
BufferedImage
,它必须单独进行。很难从你最初的问题中分辨出来,但这似乎是部分原因。启动一个新线程来进行图像处理请参阅此答案底部的我的解决方案和建议,以获得一个快速总结,也请参阅此处,以了解其他有相同问题的人:Java Creating Instance of BufferedImage Freezes Program
关于为什么你的代码可以在Windows而不是Mac上运行的简要说明:这是因为两个操作系统通常都运行不同的openGL实现,而且Mac通常会落后,错过一系列更新/更改,这些更新/更改可能会解决像这样的问题,以便在openGL线程上初始化
BufferedImage
时不会冻结如果上述方法不起作用,那么让我们先看看无头模式是什么。(我的重点):
那么什么时候应该使用无头模式呢
那不是你,是吗?但是如果那是你(LWJGL?),然后让我们看看如何使用无头模式:
这意味着你应该有一段特殊的无头代码来处理你的无头图像,然后将图像传递回一个有头的普通JFrame
那么,为什么你会失败:
问题的解决方案:
很好,所以我们只需要小心在无头模式下使用什么。您询问了如何使用和不使用headless模式,而不是在需要时使用
-Djava.awt.headless
you can do it programmatically within your code使用System.setProperty("java.awt.headless", "true");
全局设置带有VM选项的headless模式。一个JFrame
应该是正常的(不是无头的),但是你可以将一个JPanel
产生为无头的,没有问题我建议:
你创建了一个正常的主线程,没有生成JFrames的VM选项,然后使用该主线程生成一个新的子线程,并将该线程中的LWJGL位设置为无头,这样你就可以毫无问题地运行LWJGL代码,同时你仍然可以从主线程获得JFrames。请记住,确保缓冲图像不是在主LWJGL/OpenGL线程中完成的
无头信息来源: http://www.oracle.com/technetwork/articles/javase/headless-136834.html