aspose file tools*
The moose likes Servlets and the fly likes error while using BufferedImage class in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "error while using BufferedImage class in servlet " Watch "error while using BufferedImage class in servlet " New topic
Author

error while using BufferedImage class in servlet

khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
hi,
i m trying to draw a piechart using bufferedimage class using the following code

response.setContentType("image/png");
String[] slices = request.getParameterValues("slice");
String[] colors = request.getParameterValues("color");
int[] sizes = new int[slices.length];
Color[] cols = new Color[slices.length];

for(int i=0; i<slices.length; i++) {
sizes[i] = Integer.parseInt(slices[i]);
cols[i] = new Color(Integer.parseInt(colors[i],16));
}
try{
int width = Integer.parseInt(request.getParameter("width"));
int height = Integer.parseInt(request.getParameter("height"));
Color background = new Color(Integer.parseInt(request.getParameter("background"),16));
BufferedImage buffer =new BufferedImage (width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffer.createGraphics();
g2.setColor(background);
g2.fillRect(0,0,width,height);
int arc = 0;
for(int i=0; i<sizes.length; i++) {
g2.setColor(cols[i]);
g2.fillArc(0,0,width,height,arc,sizes[i]);
arc += sizes[i];
}
ServletOutputStream os = response.getOutputStream();
boolean flag=ImageIO.write(buffer, "png", os);
}
catch (Exception e){
}
it gives following error at run time
javax.servlet.ServletException: Servlet execution threw an exception
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain............................................................................................................

root cause

java.lang.NoClassDefFoundError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1041)
at pie.doGet(pie.java:53)...........................................................

can any body pls indicate what may b wrong.
also if i comment the buffer.createGraphics code an image opens in photo shop showing a black rectangle.
looking forward for some help
regards
khushi
Vijay Sharma
Ranch Hand

Joined: Feb 02, 2005
Posts: 32
I think the problem is it is not able to find class definition,So,it's minor mistake.B'coz the code seems to be right.So, the problem is locating the jar file or package realted problem.Just update ur classpath for jar files also.


Vijay Sharma
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You may need to run your app server with the Java, 'headless' option.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
thanks Ben,
but can u pls tell me how to set this "headless" option??
i m using tomcat server.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

http://www.google.com/search?hl=en&q=Tomcat+Headless&btnG=Google+Search



JAVA_OPTS-Djava.awt.headless=true
khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
hi Ben,
i put
System.setProperty( "java.awt.headless", "true" );
in the init method of my servlet n it started working.....
thanks a ton...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Hmm. I didn't know you could add it after the JVM had started.
Glad it's working for you.
juilee rawale
Greenhorn

Joined: Aug 31, 2005
Posts: 7
Hi,
I am facing similar problem. I am trying to print from JSP to a shared printer. My application server JBOSS 3, printer and the client are on different linux advanced server 3 machines.

I am getting the exception

java.lang.NoClassDefFoundError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
at java.awt.Font.initializeFont(Font.java:308)
at java.awt.Font.<init>(Font.java:344)
at dm.web.reports.TextPage.<init>(Unknown Source)
at dm.web.reports.TextFile.paginate(Unknown Source)
at dm.web.reports.PrintReportUtil.printDocument(Unknown Source)

I tried using
1. setting JAVA_OPTS to -Djava.awt.headless=true in run.sh
2. calling System.setProperty( "java.awt.headless", "true" ); in jspInit() in my jsp
but it didn't help!!!

This problem is not coming on Linux advanced server 4. But, I want to run it on Linux advanced server 3

Plzzzzzz help
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by jui rawale:
I am facing similar problem. I am trying to print from JSP to a shared printer. My application server JBOSS 3, printer and the client are on different linux advanced server 3 machines.


This is for you mate.

You can run Xvfb, the X virtual frame buffer, and then set DISPLAY=:1 in JBoss, I did for Tomcat though, environment. It's basically a mini X server. I've done this instead of using headless mode because headless was not working for me.

Works great!!

Some links on how to set Xvfb:

http://www.idevelopment.info/data/Unix/General_UNIX/GENERAL_XvfbforHPUX11_0.shtml
http://testdrive.mapinfo.com/TECHSUPP/MIPROD.NSF/0/a832a07452b9a0e385256f8000760f68?OpenDocument
http://rpmfind.net/linux/RPM/redhat/updates/5.2/alpha/XFree86-Xvfb-3.3.5-1.5.x.alpha.html


Cheers.
juilee rawale
Greenhorn

Joined: Aug 31, 2005
Posts: 7
Thanks Adeel,
I am going to try this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error while using BufferedImage class in servlet