0

I try to to open a Browser with a URL if a WebServer is called. The WebServer works fine on a Tomcat7 and I created it with Eclipse. I tested this Code on the Eclipse Server and every thing is ok and a new Browser with the URL is open.

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

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 ?

Friedrich
  • 1
  • 1
  • 3
  • What is ur target run-time system ? Is that *Windows* or *Linux* or something else ? – OO7 Nov 19 '14 at 10:29
  • I have updated my answer providing **an alternative solution to support multiple target environments & remove HeadlessException.** Have a look at it. – OO7 Nov 19 '14 at 10:41
  • it is a WindowsServer 2008 R2; I will try your alternavtive solution – Friedrich Nov 19 '14 at 10:46
  • I have also added another link which has solution to problem with **windows server 2008 R2 + RDP**. Have a look at it too. I hope it helps you to solve your problem. – OO7 Nov 19 '14 at 11:33
  • thanks for your help, but i did not resolve my problem. I found an other way, I give the URL back to the client and he open it. The Problem ist that the Server has no UI-Enviorment. – Friedrich Nov 26 '14 at 14:02

2 Answers2

0

When you test it locally it works because the local environment has a UI. I suspect the "real" tomcat doesn't have a UI, so can't perform any UI related tasks.

Why are you launching a UI on a server that nobody is ever going to see or interact with? 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.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
  • my goal is that is somebody call the webservice he is redirect to a specific WebSite (in this example just google.de), or is there a better way to call a website out of a web service? – Friedrich Nov 19 '14 at 10:33
0

From the Java doc 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. Look at Desktop 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 :

Desktop API is not supported on the current platform in this post @MightyPork has good workaround to support different OSs & remove 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. You can also try this.

Community
  • 1
  • 1
OO7
  • 2,785
  • 1
  • 21
  • 33