Link to home
Start Free TrialLog in
Avatar of gagaliya
gagaliya

asked on

dynamically generate images on serverside

hey, i need to dynamically generate images on the serverside using java. here's something i am really confused about, please clarify

the requirements to generate images on the server are:

1) headless mode
2) x graphic thing installed

Are those two the same or different? i have java 1.4 so that should solve the headless mode problem automatically right? Do i still need to worry about having x-windows installed? how do i know if xwindow is installed, how do i install it?  

i am using:
Redhat Linux 7.3
Apache 1.3.27
Tomcat 4.03
J2SDK 1.4.0


--------
I also would love to give out the 500 points to the last question related to this, if anyone can take a look at it. basically the solution was already provided to me, but i am getting exceptions while trying it out. You can find the question at:

https://www.experts-exchange.com/questions/20907938/convert-servlet-to-jsp-how.html


ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of searlas
searlas


As objects says in this and your reference question, set the java.awt.headless=true method;  I'd then check that this is being picked up with <%=GraphicsEnvironment.isHeadless()%> as mentioned in the comments to your other Q.
Avatar of gagaliya

ASKER

thanks guys, guess i will just post in this thread and split the points later in both.

<%=GraphicsEnvironment.isHeadless()%>  this returns a false

So i guess it is not in headless mode.

" try setting the following property when starting tomcat:
java.awt.headless=true"

how do i do this? currently i have <% java.awt.headless=true; %>in the beginning of my jsp file.

you mentioned i ned to do it when starting tomcat? how do i do that?

also the x window part, do i have to worry about that too? thanks!
hey i did some more test see below:

<html>
<head>
<%@ page import = "java.awt.*, java.awt.image.*, com.sun.image.codec.jpeg.*, java.util.*, java.io.*" %>
</head>
<body>

<%System.setProperty("java.awt.headless", "true");%>  
<%=GraphicsEnvironment.isHeadless()%>  //this shows as false
<%=System.getProperty("java.awt.headless")%>  //this shows as true
</body>
</html>

i still get the same exceptions
javax.servlet.ServletException
     at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:463)
     at org.apache.jsp.itemlogo$jsp._jspService(itemlogo$jsp.java:102)
     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
     at org.apache.catalina.core.ApplicationFilterChain.access$0(ApplicationFilterChain.java:197)
     at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:176)
     at java.security.AccessController.doPrivileged(Native Method)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:172)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.ajp.tomcat4.Ajp13Processor.process(Ajp13Processor.java:429)
     at org.apache.ajp.tomcat4.Ajp13Processor.run(Ajp13Processor.java:495)
     at java.lang.Thread.run(Thread.java:536)


root cause

java.lang.NoClassDefFoundError
     at java.lang.Class.forName0(Native Method)
     at java.lang.Class.forName(Class.java:130)
     at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
     at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1053)
     at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1043)
     at org.apache.jsp.itemlogo$jsp._jspService(itemlogo$jsp.java:65)
     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
     at org.apache.catalina.core.ApplicationFilterChain.access$0(ApplicationFilterChain.java:197)
     at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:176)
     at java.security.AccessController.doPrivileged(Native Method)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:172)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.ajp.tomcat4.Ajp13Processor.process(Ajp13Processor.java:429)
     at org.apache.ajp.tomcat4.Ajp13Processor.run(Ajp13Processor.java:495)
     at java.lang.Thread.run(Thread.java:536)



when trying to run this file:

<%@ page import = "java.awt.*, java.awt.image.*, com.sun.image.codec.jpeg.*, java.util.*, java.io.*" %><%

System.setProperty("java.awt.headless", "true");

// Create image
   int width=200, height=200;
   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

   // Get drawing context
   Graphics g = image.getGraphics();

   // Fill background
   g.setColor(Color.white);
   g.fillRect(0, 0, width, height);

   // Create random polygon
   Polygon poly = new Polygon();
   Random random = new Random();
   for (int i=0; i < 5; i++) {
       poly.addPoint(random.nextInt(width),
       random.nextInt(height));
   }

   // Fill polygon
   g.setColor(Color.cyan);
   g.fillPolygon(poly);

   // Dispose context
   g.dispose();

   // Send image to browser
   response.setContentType("image/jpg");
   OutputStream outputStream = new BufferedOutputStream(response.getOutputStream(), 1024);
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
   encoder.encode(image);
   outputStream.close();
%>

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

If you're on Windows you'd be editting catalina.bat this would be located somewhere like:
C:\jakarta-tomcat-4.0.3\bin\catalina.bat

You want to find the line with CATALINA_OPTS, something like:

set CATALINA_OPTS=%CATALINA_OPTS% -Dsun.io.useCanonCaches=false

and change it to:
set CATALINA_OPTS=%CATALINA_OPTS% -Dsun.io.useCanonCaches=false -Djava.awt.headless=true
As I understand from your previous post that you don't have access to servlet deployment, YOU HAVE NO PERMISSION to change the system properties and I guess you cannot change the comcat startup parameter as well.

You should talk to your admin people to add the -Djava.awt.headless=true property to tomcat startup script. If they want do it, believe me, forget about the jfreechart, you'll need other charting system that doesn't depend on AWT.

SVG is one of them as I stated in the other Q and Kava chart is another option (it's not free). http://www.ve.com/?ref=gad1
> how do i do this?

When you startup tomcat itself.
If thisn't an option then have a look at the pja toolkit.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
argg i am so confused, i asked the tech to reboot the tomcat server with the headless mode set, here's the post:

---------------
hey i need to reboot the tomcat server with the following property set:
Add CATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=true"
to the catalina.sh file  so <%=GraphicsEnvironment.isHeadless()%> will show up as true in my jsp. it shows up as false right now
--------------------------------------------------------------------------------
The line has been added to the Catalina.sh file and Tomcat4 has been restarted. Please let us know if there is anything else we can assist you with.
--------------------

so i load up the jsp..and lo behold:  headless: <%=GraphicsEnvironment.isHeadless()%>  = false  sigh. did i not tell him correctly? he didnt do it right? or what?  (#&@*#@* HEADLESS MODE! please advise, thanks.

I tried pja a while ago, it was just a disaster. I am going to try install Xvfb (XFree86-Xvfb-4.3.0-2.90.55.i386.rpm) see if it helps. But the bottomline i think is headless mode will solve my problem easily just that i cant get it to turn on (*&@#*(#@!  thanks again guys!

gaga
nevermind, dont have permission to install the rpm. /bangs head  

back to trying set headless mode.
he might have added the line to the end of the catalina.sh file :-)
ask him to add line in the beginning of the file right after the first comment section.
or you can as them to send you a copy of catalina.sh to see if they added correctly.
> so <%=GraphicsEnvironment.isHeadless()%> will show up as true in my jsp. it shows up as false right now

Also check the value of the property:

<%= System.getProperty("java.awt.headless") %>

DO you have access to the startup logs, they'd also be worth checking.
giving up on this for now, when i have more time will try more or maybe switch hosting. thanks all