Headless Mode Activation on Apache Tomcat
Java AWT/X11 errors on a server without graphical libraries go away once Tomcat runs Java in headless mode.
Explanation
Typical Java AWT/X11 exceptions are:
- Exception: Could not initialize class sun.awt.X11.XToolkit
- java.lang.InternalError: Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable
- Error number 0 in 11: Uncaught exception
- java.lang.NoClassDefFoundError: Could not initialize class java.awt.GraphicsEnvironment$LocalGE
Cause
This happens because Java tries to initialize graphical AWT/X11 components even though the server does not provide a graphical environment or X11 libraries. Enabling headless mode prevents Java from loading graphical toolkits that are not available on headless servers.
Solution
To solve this, you need to activate headless mode.
- On Linux, create a file TOMCAT_HOME/bin/setenv.sh and insert the following code:
#!/bin/sh export JAVA_OPTS="${JAVA_OPTS} -Djava.awt.headless=true" On Windows, create a file TOMCAT_HOME/bin/setenv.bat and insert the following code:
set JAVA_OPTS=%JAVA_OPTS% -Djava.awt.headless=trueWhen Tomcat runs as a Windows service, setenv.bat is not read. Set the equivalent option in the registry key below, where FOOBAR is the service name:
HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\FOOBAR\Parameters\Java
After applying the change, restart Tomcat for the setting to take effect.