Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems with java.awt.GraphicsEnvironment and sun.java2d.HeadlessGraphicsE

843807Jul 26 2009 — edited Jul 26 2009
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 23 2009
Added on Jul 26 2009
1 comment
1,155 views