如何避免线程中的HeadlessException?
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有些代码最终需要接触显卡,或者至少是 Java 中工作的图形子系统。如果您在未启用图形的系统上运行此代码,那么您将抛出 HeadlessException。
您在 Servlet 中运行代码,Servlet 是通常返回网页的代码块。由于网页是包含所有正确标签的一大串,因此网页不需要图形环境。该字符串由网络浏览器接收,并且网络浏览器通常具有图形环境来显示结果。
在您的 Web 服务器上,您要求网页生成器(Servlet)打开一个对话框。由于多种原因,这会产生问题:
所有这些点结合起来意味着 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:
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.
Java Servlet 代码在 Web 服务器上运行,而不是在 Web 客户端(Web 浏览器)上运行。 Web 服务器所做的就是监听 HTTP 请求,生成 HTML/CSS/JS 响应并将其发送到 Web 客户端。 Web 客户端所做的只是发送 HTTP 请求并处理检索到的 HTML/CSS/JS 响应。
如果在 servlet 中执行 Swing GUI,它将显示在 Web 服务器中,而不是 Web 客户端中。
对于这个特定问题,基本上有 3 种解决方案:
改为在 Web 客户端运行 Swing GUI 代码。您可以使用 Applet 或 Web Start 由 JSP/HTML 页面提供服务。
改用客户端编程/脚本语言,例如 JavaScript 或 ActionScript (Flash)。在 JavaScript 中,有一个
alert()
函数可以显示对话框。在 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:
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.
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.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.