I am working with some code that writes animated output to the desktop using AWT and Swing features. It draws using 2D graphics and renders text in fonts.
This code can use the Java Media Framework to save the animationto movie files.
I would like to port this code to a pure server-side environment, for working with a Web UI. The drawing code could either run inside the servlet container, or outside as a command-line program. (Or I could opt not to use a servlet container and instead use the newer ways of running Java web servers from the command line.)
Can I use AWT and Swing drawing features without starting the X Window System on the server?
Advertisement
Answer
The article Using Headless Mode in the Java SE Platform outlines the limitations imposed on such applications.
As a concrete example JFreeChart
is a graphic program widely used in both desktop and servlet contexts. For the latter, any of several ChartUtils
may be used to stream rendered content in a headless environment.
Alternatively, although deprecated, consider Java Web Start to deploy an existing Swing application, for example the JFreeChart
demo seen here.
Addendum: This Oracle forum thread suggests that the Java Media Framework can indeed throw HeadlessException
; a suggested workaround is to specify a particular system property value:
-Dawt.toolkit=sun.awt.HeadlessToolkit
This JFreeChart
forum thread suggests Xvfb
or vnc
as alternatives when headless
mode is not available.