Did you ever fought a HeadlessException?

Once I wrote some JUnit tests for a framework which referenced GUI components in the model. No worries on my development machine - but as I moved those tests to the test server I’ve got that bloody HeadlessException. At this very moment I felt a bit headless too.

But don’t panic - here is a quick ‘n easy solution (at least if your running a Linux/Unix like OS, well you won’t run into this problem on a Windows machine).

The trick is to run a virtual framebuffer X server (Xvfb) which can run on machines with no display hardware and no physical input devices.

#!/bin/bash
# Start fake X server
/usr/bin/X11/Xvfb :1 -screen 0 800x600x16  &
echo "Xvf started"# Export display
export DISPLAY=:1.0

# Now, start your application, tests or whatsoever...
java -jar mytests net.n0fx.TestSuite

# Kill the fake X server after your done
PID=`ps -fu ${LOGNAME} | grep 'Xvf' | grep -v grep | awk '{print $2}'`

if [ "$PID" != "" ]; then
   kill -9 $PID
   echo "Xvf stopped";
else
   echo "Xvf is not running";
fi

Leave a Reply

You must be logged in to post a comment.