3

I need to do some image processing on a java server (Debian with java version "1.6.0_12"), and I am receiving java.awt.HeadlessException from my code:

java.awt.HeadlessException
    at sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:64)
    at WaxOn.getDefaultConfiguration(WaxOn.java:341)

Even when java.awt.headless is set to true (as evident by this code printing so):

if (!java.awt.GraphicsEnvironment.isHeadless())
{
    logger.warn("Headless mode is not enabled");
}
else
{
    logger.info("Headless mode");
}

This is the code that throws the exception:

public static GraphicsConfiguration getDefaultConfiguration()
{
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    return gd.getDefaultConfiguration();
}

Any idea how to solve this?

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87

2 Answers2

5

When headless you don't have a screen device. The documentation is clear:

Throws: HeadlessException - if isHeadless() returns true

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • When isHeadless is false, the jvm attempts to connect to the X server. headless mode supposed to support some operations without an X server. – Omry Yadan Dec 26 '10 at 16:52
  • This is also very clear: http://java.sun.com/products/java-media/2D/reference/faqs/index.html , look for 'headless' in that document. – Omry Yadan Dec 26 '10 at 16:55
  • Okay. how do I perform a bicubic image resize without an (god this is stupid) x server in Java? – Omry Yadan Dec 26 '10 at 16:57
  • The documentation of the method says that it must throw exception if in headless mode. So there's nothing more to that - it just is not possible. If you feel this is wrong, you can create an issue in the oracle bug tracking system. – Bozho Dec 26 '10 at 16:58
  • @Omry - that is another question - feel free to ask it as a new one ;) – Bozho Dec 26 '10 at 16:59
1

Your code appears to be getting the default graphics device configuration on a machine that doesn't have a usable graphic device. This doesn't make much sense, and is never going to work.

If you could explain why you are trying to do this (on a headless machine) we might be able to offer an alternative.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    I am trying to resize and crop an image. it makes perfect sense to be able to do this without an X server. – Omry Yadan Dec 26 '10 at 16:53
  • 1
    and btw: the mare fact that there is a class named HeadlessGraphicsEnvironment (see the stack trace) suggest that there is a good chance that this will actually work. – Omry Yadan Dec 26 '10 at 17:05