Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

problem with java2d in linux vps

  Asked By: Chisisi    Date: Jun 06    Category: Java    Views: 1073

in my web application i neet to change the size of some pictures and i use the below method for resizing pictures. when i run the application under windows there is no problem but when i run it under my vps (linux) the mehtod does not work (the method returns null).
i would like to know if it is the problem with linux and is there any solution for it.

public static byte[] getImage(byte[] imageBytes, String format, int
previewWidth, int previewHeight) {

ByteArrayOutputStream result = new ByteArrayOutputStream();

int newWidth = previewWidth;

int newHeight = previewHeight;


ByteArrayInputStream byteInput = new ByteArrayInputStream(imageBytes);

BufferedImage image = ImageIO.read(byteInput);

if (image.getWidth() <= previewWidth && image.getHeight() <=
previewHeight) // picture is so small that it fits as preview itself

return imageBytes;

BufferedImage newImage = new BufferedImage(newWidth, newHeight,

Graphics2D g2d = (Graphics2D) newImage.createGraphics();


g2d.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, image.getWidth(),
image.getHeight(), new JFrame());


ImageIO.write(newImage, format, result);

} catch (Exception ex){}

return result.toByteArray();




6 Answers Found

Answer #1    Answered By: Josie Roberts     Answered On: Jun 06

It seems that you haven’t any X11 installed on your linux  box, I think the problem  is that java AWT uses native OS API for graphic works and you don’t have any.

Your have two solution  one is to install X11 on your linux and another is to run  your JVM with headless=true option.

But take care that the first solution is not safe for internet servers because you make a security whole for it.

Answer #2    Answered By: Marc Anderson     Answered On: Jun 06

First of all, never eat (ignore) exceptions*. In other words, don't do

try {
// some code
catch  (AnException e) {}

Besides that never catch Exception:

try {
// some code
} catch (Exception e) { // handle e }

In your code you are doing both together:

try {
// your code
} catch (Exception e) {}

Now your question. Please fix your code and when you catch the
exception, log its stack trace or print it to System.out. Besides that
what is your Java version (the output of java -version)?

Answer #3    Answered By: Kiet Jainukul     Answered On: Jun 06

No so sure, but as you are running in a VPS Linux box (and probably
without X) you may need to run  in headless mode enabled.

From JDK 1.4, Java2D has built in support for headless graphics and
automatically switches to those implementations, when enabled via

Also don't forget to have headless mode graphics libraries installed (and
pointed by LD_LIBRARY_PATH).

Answer #4    Answered By: Mae Roberts     Answered On: Jun 06

my web  application is running under tomcat 5.5, jdk 6 update 2, Centos 5 linux  without x11 server installed.

i also use the below command :
JAVA_OPTS="-Xms16m -Xmx48m -Djava.awt.headless=true"

but when i call the method  in "linux" i get the below exception:

at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.<init>(Window.java:406)
at java.awt.Frame.<init>(Frame.java:402)
at java.awt.Frame.<init>(Frame.java:367)
at javax.swing.JFrame.<init>(JFrame.java:163)
at mypackage.MyClass.getImage(MyClass.java:103)

and the the line which produces exception  in 'getImage()' is:

g2d.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, image.getWidth(), image.getHeight(), new JFrame());
in spite of setting headless property to true i get the error.
any Idea?

Answer #5    Answered By: Freda Lane     Answered On: Jun 06

You'd better included the exception  message along with stack trace, but I
bet it's this one:

java.awt.HeadlessException: No X11 DISPLAY variable was set, but this
program performed an operation which requires it.

Well although headless API can do much of the graphics logic without
really having one, but don't except it to draw the image  too :)

IMHO your problem  is because you've tried to draw this image on a new
JFrame (which does not exist in fact). I'm not sure if that line
(drawImage) is necessary any more (since you've already defined your new
image's width and height) but if really needed, it should be replaced by
another command avoiding to draw it.

Answer #6    Answered By: Hooriya Khan     Answered On: Jun 06

It may be because one of your methods is not supported in headless mode try to find a replacement for it.

It may be JFrame constructor.

Reading this Article may be helpful.


Didn't find what you were looking for? Find more on problem with java2d in linux vps Or get search suggestion and latest updates.