Simple Captcha: java.awt.HeadlessException
September 26th, 2007
If you are trying to use the SimpleCaptcha library on a Unix server without an X-Server running, it is most likely that you will end up getting a java.awt.HeadlessException. That happens because SimpleCaptcha tries to acquire information about the system's default screen, without particular reason.
Fortunately, SimpleCaptcha ships with its source code. What you can do is to edit the nl.captcha.text.imp.DefaultWordRenderer class, and remove the bogus lines of code. This is the original code (around line 53):
Simply comment out the first three lines of code. These variables are not used anywhere below this point, so it is safe to take them off. The code should now look like this:
Build the library and package it in a .jar file and use it. It should be working fine.
You will also have to make sure that you have the X11 libraries installed on your system (even though an X server is not running), and you will also have to invoke the jvm in Headless mode by setting the java.awt.headless system property to true. One easy way to do this is to include it in the command line options of the jvm:
If you plan to use the SimpleCaptcha library within a Tomcat web application (which is the most likely case), make sure that you include the above command line option in Tomcat's invocation command.
Fortunately, SimpleCaptcha ships with its source code. What you can do is to edit the nl.captcha.text.imp.DefaultWordRenderer class, and remove the bogus lines of code. This is the original code (around line 53):
public BufferedImage renderWord (String word, int width, int height) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = image.createGraphics();
g2D.setColor(Color.black);
...Simply comment out the first three lines of code. These variables are not used anywhere below this point, so it is safe to take them off. The code should now look like this:
public BufferedImage renderWord (String word, int width, int height) {
//GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//GraphicsDevice gd = ge.getDefaultScreenDevice();
//GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = image.createGraphics();
g2D.setColor(Color.black);
...Build the library and package it in a .jar file and use it. It should be working fine.
You will also have to make sure that you have the X11 libraries installed on your system (even though an X server is not running), and you will also have to invoke the jvm in Headless mode by setting the java.awt.headless system property to true. One easy way to do this is to include it in the command line options of the jvm:
java -Djava.awt.headless=true ...If you plan to use the SimpleCaptcha library within a Tomcat web application (which is the most likely case), make sure that you include the above command line option in Tomcat's invocation command.