java.awt.HeadlessException - 未显示Applet - 第2部分

时间:2022-06-06 11:12:09

This is with reference to question java.awt.HeadlessException - Applet not displayed. java.awt.HeadlessException - Applet not displayed

这是参考问题java.awt.HeadlessException - 不显示Applet。 java.awt.HeadlessException - 未显示Applet

The HeadlessException went away after I added "export DISPLAY=:0.0" in tomcat's startup.sh file. Now some part of the code is run on a batch server which is a separate server. The same HeadlessException occurs when the code is invoked on the batch server. When I added the same "export DISPLAY=:0.0" in batch server's startup file, the exception went away but it created problem for other (non java) applications running on the batch server. This is probably because we are overriding the display that is working for other applications.

在tomcat的startup.sh文件中添加“export DISPLAY =:0.0”后,HeadlessException消失了。现在代码的某些部分在批处理服务器上运行,该服务器是一个单独的服务器。在批处理服务器上调用代码时会发生相同的HeadlessException。当我在批处理服务器的启动文件中添加相同的“export DISPLAY =:0.0”时,异常消失了,但它为批处理服务器上运行的其他(非Java)应用程序创建了问题。这可能是因为我们正在覆盖适用于其他应用程序的显示器。

Next I exported JAVA_OPTS="-Djava.awt.headless=true" to batch server's startup file, but it did not work. I saw that the following link from Sun says that code should check for headless exception.

接下来,我将JAVA_OPTS =“ - Djava.awt.headless = true”导出到批处理服务器的启动文件,但它不起作用。我看到来自Sun的以下链接说代码应检查无头异常。

http://java.sun.com/j2se/1.4.2/docs/guide/awt/AWTChanges.html

So will catching the headless exception make it work? Like:

那么抓住无头的异常会让它发挥作用吗?喜欢:

try {

 //Code that throws headless exception

} catch (HeadlessException e) {

  printStacktrace(); //basically do nothing
}

1 个解决方案

#1


You could catch that exception or you could avoid it by checking first:

您可以捕获该异常,或者您可以通过先检查来避免它:

if (! java.awt.GraphicsEnvironment.isHeadless()) {
    // code that throws headless exception
} else {
  log.info("Skipping GUI portion")
}

Don't set a DISPLAY on a server that no one is going to look at, or you might end up waiting forever for someone to click OK on a dialog that no one can see.

不要在没有人会看的服务器上设置DISPLAY,或者你可能最终等待某人在没有人能看到的对话框上单击OK。

#1


You could catch that exception or you could avoid it by checking first:

您可以捕获该异常,或者您可以通过先检查来避免它:

if (! java.awt.GraphicsEnvironment.isHeadless()) {
    // code that throws headless exception
} else {
  log.info("Skipping GUI portion")
}

Don't set a DISPLAY on a server that no one is going to look at, or you might end up waiting forever for someone to click OK on a dialog that no one can see.

不要在没有人会看的服务器上设置DISPLAY,或者你可能最终等待某人在没有人能看到的对话框上单击OK。