0

I use this code successfully in a Java desktop application (JUnit tests in NetBeans) to generate SVG from WMF data:

InputStream wmfStream = new ByteArrayInputStream(wmfBytes);
WMFTranscoder transcoder = new WMFTranscoder();
TranscoderInput wmf = new TranscoderInput(wmfStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
TranscoderOutput svg
        = new TranscoderOutput(new OutputStreamWriter(bos, "UTF-8"));
transcoder.transcode(wmf, svg);
return bos.toByteArray();

If I execute the conversion in GlassFish, a headless exception is thrown:

java.awt.HeadlessException at sun.awt.HeadlessToolkit.getScreenResolution(HeadlessToolkit.java:224) at org.apache.batik.transcoder.ToSVGAbstractTranscoder.(ToSVGAbstractTranscoder.java:96) ... 33 more

How can I use Apache Batik WMF to SVG conversion in a 'headless' Java runtime environment?


The accepted answer Having problems with Apache Batik library on AWS ElasticBeanstalk Instance suggests to use a Sun JDK. However, I am already running GlassFish 4.0.1 on Oracle JDK 1.7 u 65.

Community
  • 1
  • 1
mjn
  • 36,362
  • 28
  • 176
  • 378
  • Can you just add -Djava.awt.headless=true to your JAVA_OPTS – Mike K. Aug 27 '14 at 15:19
  • @MikeK. if I query the property at run time - before the conversion - it is already `true`, so I guess setting it at startup does not make a difference? – mjn Aug 27 '14 at 16:13
  • well if you really can't install x windows you could set up x virtual frame buffer xvfb and then set your DISPLAY environment variable to use that. – Mike K. Aug 27 '14 at 16:27

2 Answers2

0

Presumably WMF uses fonts or such, that a headless environment does not provide. Headless meaning, a (Linux) server without GUI (X server), on which to rely for font rendering and the rest. Installing an X server Linux package should do.

Under debian alikes

apt-get install x-window-system

So the adage "for a server one does not need a graphical window manager" unfortunately is not true.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

Actually it is easy to solve - at least on my development machine, GlassFish configuration (domain.xml) needs to be changed to set

<jvm-options>-Djava.awt.headless=false</jvm-options>

Now testing on Linux with x-window-system, and will be back later with results.

mjn
  • 36,362
  • 28
  • 176
  • 378