Resize image in Java

Posted on 2005-02-24
Medium Priority
Last Modified: 2008-01-09
Hi All!!!
Now I'm developping a web application, which run on a JBoss server with jsp/struts.
In my app, I need resize some image.I use java AWT for this, it's ok in my enviroment. but in the customer enviroment it throws an exception:
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
after play with google, I found It caused by a AWT bug when run in Linux so I dont want to use AWT to resize the image.

Could anyone show me  the solution? or show me what's my mistake when use AWT?

My code:
public void doResize (String inPath,OutputStream os, int targetWidth,int targetHeight) throws CBException {

       Image sourceImage = (Toolkit.getDefaultToolkit().getImage(inPath));

        BufferedImage resizedImage = this.scaleImage(sourceImage,targetWidth,targetHeight);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
        try {
        } catch (IOException e) {
            throw new CBException("Exception when encode image to JPG");

private BufferedImage scaleImage(Image sourceImage, int width, int height){
            ImageFilter filter = new ReplicateScaleFilter(width,height);
            ImageProducer producer = new FilteredImageSource(sourceImage.getSource(),filter);
            Image resizedImage = Toolkit.getDefaultToolkit().createImage(producer);
            return this.toBufferedImage(resizedImage);
     * convert a Image object to BufferredImage
     * @param image
     * @return
 private BufferedImage toBufferedImage(Image image){
            image = new ImageIcon(image).getImage();
            BufferedImage bufferedImage = new BufferedImage(image.getWidth(null)
            Graphics g = bufferedImage.createGraphics();
            return bufferedImage;

Question by:Lara_tin2
  • 2
LVL 92

Assisted Solution

objects earned 750 total points
ID: 13390643
not really a bug, it is caused by there being no window environment running on the box (in this case X windows)

you could try using a headless implementation by adding the following VM arg to the JBoss startup option.


Or an alternative to AWT is the PJA toolkit


Accepted Solution

jjerome00 earned 750 total points
ID: 13395698

I'm interested in where your production environment is.  Most hosting companies won't allow you to resize an image this way because it would take too much resources from the Server.  I mention this because I used an image tag lib from Apache for something similar to this, then came across the same issue.

BTW, the image tag library that does some pretty cool image resizing and so forth.  It's still in development, but it works great.  You might want to check it out:


Author Comment

ID: 13399827
Thanks for all.
Resize image is a common operation when use image so I think it must have a  java library.But I cant find anything :(.
What I want is an easy-to-use and light weight java library to do it.If you know one, please let me know.
objects:and jjerome00 , I will try your comment and feedback later.
LVL 92

Assisted Solution

objects earned 750 total points
ID: 13400512
> Resize image is a common operation when use image so I think it must have a  java library

It does, the code you have is fine.
Here are some other examples:

The error is caused because there is no windows environment running on the box.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a smartphone, I'm sure you've noticed that web applications are changing. Yet, are you aware of how these changes are going to impact your organization or company?
Disk errors can be the source of sundry problems for the Exchange server, the most common one being that the database fails to mount.
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question