I've a server running Ubuntu server edition. Therefore it is not running any X11 server. My webapp performs some image rescaling of images that a user uploads. For testing purpose i have a test that automatically checks if the images are transformed correctly. But i get the following error message:
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:82)
at parken.server.util.ImageUtil.getDefaultConfiguration(ImageUtil.java:11)
After some googling i added the following to my .bashrc
DISPLAY=:0.0
export DISPLAY
Now i get the following exception:
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:62)
sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:166)
java.security.AccessController.doPrivileged(Native Method)
sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:142)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:186)
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
parken.server.util.ImageUtil.getDefaultConfiguration(ImageUtil.java:10)
The class in wich the error occurs looks like this:
package parken.server.util;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
public class ImageUtil {
private static GraphicsConfiguration getDefaultConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); //<-this ist line 10
GraphicsDevice gd = ge.getDefaultScreenDevice(); //<-this ist line 11
return gd.getDefaultConfiguration();
}
private static BufferedImage copy(BufferedImage source, BufferedImage target) {
Graphics2D g2 = target.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
double scalex = (double) target.getWidth()/ source.getWidth();
double scaley = (double) target.getHeight()/ source.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(scalex, scaley);
g2.drawRenderedImage(source, at);
g2.dispose();
return target;
}
private static BufferedImage getScaledInstance(BufferedImage image, int width, int height, GraphicsConfiguration gc) {
if (gc == null)
gc = getDefaultConfiguration();
int transparency = image.getColorModel().getTransparency();
return copy(image, gc.createCompatibleImage(width, height, transparency));
}
public static BufferedImage rescaleImage( BufferedImage img, int width , int height ) {
return getScaledInstance( img , width, height, ImageUtil.getDefaultConfiguration() );
}
}
Acutally i don't have any idea how to repair this.