I have managed to break a working application, and cannot work out how to fix it.
I have a java web-app which runs on "tc server" on a Windows7 box from within Eclipse (spring STS version.)
The app will use java.awt.robot to perform a screen capture, and display the captured image on the subsequent page.
So far so good, and I have been able to collect a bufferedImage from the screen and display it on a subsequent page within the web-app.
And then I somehow broke the web-app, possibly be loading the Android SDK and Eclipse tools.
Now, I am getting an exception from Robot because it cannot run in a HeadlessEnvironment. Google searches indicate that a headlessEnvironment is for running without a screen and keyboard, but I have both. The application worked previously without ever encountering the HeadlessEnvironment.
If I debug and display the GraphicsEnvironment;
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
I get a HeadlessGraphicsEnvironment object based on a Win32GraphicsEnvironment.
whilst:
String nm = java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction("java.awt.graphicsenv", null));
System.out.println("java.awt.graphicsenv = " + nm);
gives
java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
My poor upgrade to android led me to originally abandon the upgrade and reinstall the latest SpringSource toolset. This encountered the HeadlessEnvironment problem, so I have reverted back to my original version of eclipse, which now also has the same problem.
I feel rather stuck now, and as most searches are telling me how I can turn Headless mode ON, can anyone explain how to turn headless mode back off again ?
Many Thanks !
<------------- EDIT: Stack Trace Added ------------------------------------------------------>
05-Apr-2012 18:42:51 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [viewerServlet] in context with path [/WebView] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: java.awt.AWTException: headless environment] with root cause
java.awt.AWTException: headless environment
at java.awt.Robot.<init>(Robot.java:75)
at uk.co.fred.MSWindowsHandler.WindowHandlerUtilities.setupRobot(WindowHandlerUtilities.java:191)
at uk.co.fred.MSWindowsHandler.WindowHandlerUtilities.scanWindow(WindowHandlerUtilities.java:71)
at uk.co.fred.Scanner.ScanServiceImpl.scanScreenToBufferedImage(ScanServiceImpl.java:66)
at uk.co.fred.viewer.controller.scan.control.ScanControlController.postPage_control_Scan(ScanControlController.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.traceNextValve(HttpRequestOperationCollectionValve.java:112)
at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:94)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:279)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Issue Resolved: Within the Server launch configuration within Eclipse there are a set of configuration parameters which included the statement:
-Djava.awt.headless=true
By creating another Server configuration in Eclipse and selecting a "Base" type instead of an "Insight" type of tc Server Instance, I was able to get a not Headless Environment.
It seemed like the "Insight" type for tc server insisted on a Headless run environment (although I did not investigate further.)
Since you mentioned "web-app", does the app server run as a windows service? If you were in the services panels, there is an "interact with desktop" checkbox that can cause your symptom.
I had an issue with this when switching to a new SSH client. I'd always used putty with no issue. With Putty, Java would run in headless mode. When I used MobaXTerm, Java tried to connect to the X11 server. I'm sure there is a setting somewhere that would fix MobaXTerm so this doesn't happen, and also a setting in Putty that could cause the same problem.
I used this code (thanks OP) to verify that this was my problem:
import java.awt.GraphicsEnvironment;
public class GetGraphics{
public static void main(String[] args){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
String nm = java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction("java.awt.graphicsenv", null));
System.out.println("java.awt.graphicsenv = " + nm);
}
}
First ran it on server connected with MobaXTerm:
$javac GetGraphics.java
$java GetGraphics
I got this error:
Xlib: connection to "localhost:10.0" refused by server
Xlib: MobaXterm X11 proxy: unable to connect to forwarded X server: Network error: Connection refused
Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable.
.... More stack trace ....
Now I'll set headless to true and run again, still using MobaXTerm:
$ java -Djava.awt.headless=true GetGraphics
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
Now finally I can run this using putty like this:
$ java GetGraphics
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
Conclusion: The client you use to connect to the server can cause issues with graphics. In my case graphs using JFreeChart stopped working. Switching back to putty solved this.