如何避免线程中的HeadlessException?

发布于 2024-09-11 14:19:29 字数 883 浏览 4 评论 0原文

我尝试在 Servlet & 中打开一个对话框它打开得很好。 但后来我尝试在线程的 run 方法中实现同样的目标。 它给了我以下错误:

java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.<init>(Window.java:431)
at java.awt.Frame.<init>(Frame.java:403)

下面是我的代码:

JFrame frame = new JFrame("Success Message");
frame.setSize(200, 50);
frame.add(new JLabel("Data uploaded from "+inputFile.getFilename()));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

我也尝试了下面的代码,但失败的

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
System.out.println("Headless mode: " + ge.isHeadless());
if(!ge.isHeadless()){ 
    System.setProperty("java.awt.headless", "true");
}

异常描述为: 当在不支持键盘、显示器或鼠标的环境中调用依赖于键盘、显示器或鼠标的代码时抛出该错误。

I have tried to open a dialog box in Servlet & it opens fine.
But then I tried to achieve same thing in my thread's run method.
It gaved me following error:

java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.<init>(Window.java:431)
at java.awt.Frame.<init>(Frame.java:403)

Below is my code :

JFrame frame = new JFrame("Success Message");
frame.setSize(200, 50);
frame.add(new JLabel("Data uploaded from "+inputFile.getFilename()));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I also tried below code, but failed

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
System.out.println("Headless mode: " + ge.isHeadless());
if(!ge.isHeadless()){ 
    System.setProperty("java.awt.headless", "true");
}

Exception is described as :
Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-09-18 14:19:30

有些代码最终需要接触显卡,或者至少是 Java 中工作的图形子系统。如果您在未启用图形的系统上运行此代码,那么您将抛出 HeadlessException。

您在 Servlet 中运行代码,Servlet 是通常返回网页的代码块。由于网页是包含所有正确标签的一大串,因此网页不需要图形环境。该字符串由网络浏览器接收,并且网络浏览器通常具有图形环境来显示结果。

在您的 Web 服务器上,您要求网页生成器(Servlet)打开一个对话框。由于多种原因,这会产生问题:

  1. 对话框将显示在 Web 服务器上,而不是 Web 客户端上。
  2. Web服务器只有一个屏幕,会根据浏览网页的人弹出对话框。
  3. 由于服务器可能同时处理许多客户端,因此服务器被调整为每个客户端使用最少量的资源。
  4. 由于服务器仅将项目返回到 Web 浏览器,因此服务器不需要图形环境。

所有这些点结合起来意味着 servlet 将不会被配置为有权访问图形环境,并且将没有机会显示弹出对话框;因为,没有可用于显示对话框的图形环境。

一般来说,您不能将 swing / awt 代码与 servlet 混合使用;然而,swing 和 awt 中都提供了图形操作的子集,它们允许在不需要图形环境的情况下进行图像操作。这是为了简化在文件处理环境中转换和构建图像的开发,在文件处理环境中,程序永远不会显示图像。以 .png 到 .jpg 转换器为例,只要它从不显示图像,程序就可以打开图像来完成其工作,并关闭图像,而无需显卡。

There is code which eventually needs to touch a graphics card, or at least a working graphics subsystem in Java. If you run this code on a system which doesn't have graphics enabled, then you will throw a HeadlessException.

You are running your code in a Servlet, which is a chunk of code that typically returns a web page. Since the web page is one big string containing all of the proper tags, the web page doesn't need a graphical environment. That string is received by a web browser, and the web browser generally has a graphical environment to display the results.

On your web server, you are asking the web page generator (the servlet) to open up a dialog box. This creates problems for a number of reasons:

  1. The dialog box would show up on the web server, not on the web client.
  2. The web server has only one screen, and the dialog boxes will pop up according to the people browsing the web page.
  3. Since the server potentially might handle many clients simultaneously, the server is tuned to use a minimum amount of resources for each client.
  4. Since the server only returns items to web browsers, the server doesn't need a graphical environment.

All of these points combined means that the servlet won't be configured to have access to a graphics environment, and there will be no opportunity to display a pop-up dialog; because, there is no graphical environment available to display a dialog.

In general, you can't mix swing / awt code with servlets; however, there is a subset of Graphics operations available in both swing and awt which allow image manipulation without needing a graphical environment. This is to ease the development of transforming and building images in a file processing environment, where the images will never be displayed by the program. Think of a .png to .jpg converter as an example, provided it never shows the image, the program could open Image(s) do its work, and close the images without needing a graphics card.

马蹄踏│碎落叶 2024-09-18 14:19:30

Java Servlet 代码在 Web 服务器上运行,而不是在 Web 客户端(Web 浏览器)上运行。 Web 服务器所做的就是监听 HTTP 请求,生成 HTML/CSS/JS 响应并将其发送到 Web 客户端。 Web 客户端所做的只是发送 HTTP 请求并处理检索到的 HTML/CSS/JS 响应。

如果在 servlet 中执行 Swing GUI,它将显示在 Web 服务器中,而不是 Web 客户端中。

对于这个特定问题,基本上有 3 种解决方案:

  1. 改为在 Web 客户端运行 Swing GUI 代码。您可以使用 AppletWeb Start 由 JSP/HTML 页面提供服务。

  2. 改用客户端编程/脚本语言,例如 JavaScript 或 ActionScript (Flash)。在 JavaScript 中,有一个 alert() 函数可以显示对话框。

  3. 在 JSP 中使用 JSTL 和/或 EL 等标记库来有条件地呈现 HTML/CSS/JS 内容。最终可以与解决方案 #2 结合使用。

Java servlet code runs at webserver, not at webclient (webbrowser). All the webserver does is listening on HTTP requests, producing HTML/CSS/JS responses and sending it to the webclient. All the webclient does is sending HTTP requests and processing the retrieved HTML/CSS/JS responses.

If you execute Swing GUI in the servlet, it will be displayed in the webserver, not in the webclient.

There are basically 3 solutions for this particular problem:

  1. Run Swing GUI code at webclient instead. You can do that in flavor of an Applet or Web Start which get served by a JSP/HTML page.

  2. Use a client side programming/scripting language instead, e.g. JavaScript or ActionScript (Flash). In JavaScript there's an alert() function which displays a dialog.

  3. Use taglibs like JSTL <c:if> and/or EL in JSP to render HTML/CSS/JS content conditionally. Can eventually be combinied with solution #2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文