• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error while using BufferedImage class in servlet

 
khushi paul
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may need to run your app server with the Java, 'headless' option.
 
khushi paul
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ben,
but can u pls tell me how to set this "headless" option??
i m using tomcat server.
 
khushi paul
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. I didn't know you could add it after the JVM had started.
Glad it's working for you.
 
juilee rawale
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adeel,
I am going to try this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic