简体   繁体   English

Java WebService上的java.awt.HeadlessException

[英]java.awt.HeadlessException on a Java WebService

I try to to open a Browser with a URL if a WebServer is called. 如果调用了WebServer,我尝试用URL打开浏览器。 The WebServer works fine on a Tomcat7 and I created it with Eclipse. WebServer在Tomcat7上运行良好,我是用Eclipse创建的。 I tested this Code on the Eclipse Server and every thing is ok and a new Browser with the URL is open. 我在Eclipse服务器上测试了此代码,一切正常,并且打开了带有URL的新浏览器。

public java.lang.String register(java.lang.String username, java.lang.String password) throws java.rmi.RemoteException {
    try{
        Desktop.getDesktop().browse(new URI("http://google.de"));
    }catch(Exception e){ 
           StringWriter sw = new StringWriter();
           e.printStackTrace(new PrintWriter(sw));
           String exceptionAsString = sw.toString();
           username = exceptionAsString;
    }  
    return "NEW TOKEN:"+password + username;
}

But if I deploy the code as a war-file to the "real" TomcatServer I get this error: (the webservice is ok and i become the return value, but the new Browser is not open) The Error is thrown, because the desktop is not supported Desktop.isDesktopSupported() == false on the "real" Server 但是,如果我将代码作为war文件部署到“真实的” TomcatServer中,则会收到此错误:(Web服务正常,并且我成为返回值,但新的浏览器未打开),将引发错误,因为桌面在“真实”服务器上不支持Desktop.isDesktopSupported() == false

java.awt.HeadlessException at java.awt.Desktop.getDesktop(Desktop.java:124)
....

My question is now, why I get this error when everything works fine on the test system and how could I resolve this problem ? 我的问题是,现在在测试系统上一切正常时,为什么会出现此错误?我该如何解决此问题?

When you test it locally it works because the local environment has a UI. 在本地对其进行测试时,它可以工作,因为本地环境具有UI。 I suspect the "real" tomcat doesn't have a UI, so can't perform any UI related tasks. 我怀疑“真正的” tomcat没有UI,因此无法执行任何与UI相关的任务。

Why are you launching a UI on a server that nobody is ever going to see or interact with? 为什么要在没有人看到或交互的服务器上启动UI? What are you trying to achieve? 您想达到什么目的? You need to code a non interactive way of getting the task done. 您需要编写一种非交互方式来完成任务。

Maybe you could also check out some of the other questions on here about HeadlessException . 也许您还可以在此处查看有关HeadlessException的其他一些问题。

From the Java doc HeadlessException 来自Java文档HeadlessException

HeadlessException : 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.

You are calling Desktop.getDesktop() method & it is not supporting your run-time environment. 您正在调用Desktop.getDesktop()方法,并且它不支持您的运行时环境。 Look at Desktop getDesktop() 查看桌面getDesktop()

Instead of checking 而不是检查

Desktop.isDesktopSupported() == false

you can use this 你可以用这个

if(!Desktop.isDesktopSupported()){
    //do someting
}

Solution to support multiple target environment & get rid of HeadlessException : 支持多个目标环境并摆脱HeadlessException的解决方案:

Desktop API is not supported on the current platform in this post @MightyPork has good workaround to support different OSs & remove HeadlessException. 本文中的当前平台不支持桌面API@ MightyPork有很好的解决方法来支持不同的操作系统并删除HeadlessException。 You must have a look at this. 你必须看看这个。

@Balazs Gunics is also facing the same problem with windows server 2008 R2 + RDP & he got the solution with @Andrea Turri answer given at Opening an URL from java . @Balazs GunicsWindows Server 2008 R2 + RDP上也面临着同样的问题 ,他从打开Java的URL中 获得了@Andrea Turri的答案 You can also try this. 您也可以尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM