有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    解决这个问题的最佳方法可能是回到过去,以不同的方式解决最初的问题

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

    请参阅此答案底部的我的解决方案和建议,以获得一个快速总结,也请参阅此处,以了解其他有相同问题的人:Java Creating Instance of BufferedImage Freezes Program


    关于为什么你的代码可以在Windows而不是Mac上运行的简要说明:这是因为两个操作系统通常都运行不同的openGL实现,而且Mac通常会落后,错过一系列更新/更改,这些更新/更改可能会解决像这样的问题,以便在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.

    很好,所以我们只需要小心在无头模式下使用什么。您询问了如何使用和不使用headless模式,而不是在需要时使用-Djava.awt.headlessyou 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