• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1308
  • Last Modified:

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:

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20907938.html


0
gagaliya
Asked:
gagaliya
  • 5
  • 3
  • 3
  • +1
3 Solutions
 
objectsCommented:
> so that should solve the headless mode problem automatically right?

I think you need to set the following property when starting tomcat:
java.awt.headless=true

also have a look at pja toolkit:
http://www.eteks.com/pja/en/
0
 
searlasCommented:

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.
0
 
gagaliyaAuthor Commented:
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gagaliyaAuthor Commented:
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();
%>

0
 
searlasCommented:
From the Tomcat HowTo... this is how you need to set the headless flag.  It needs to be set at startup; calling System.setProperty(...) is too little too late by the time the server's running:

If your webapp displays graphics and you don't want to start the Linux X server after a boot-up, you can modify the catalina.sh file and set the 'headless' flag to true. This part is optional depending your webapp. My program does display graphics so this is what I did for each of my tomcat instances.

$ cd /usr/local/sites/tom1/tomcat/bin
Add CATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=true" to the catalina.sh file.

From section 3.5:
http://www.cardon.biz/docs/tomcat/install_tomcat.html
0
 
searlasCommented:

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
0
 
kennethxuCommented:
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
0
 
objectsCommented:
> how do i do this?

When you startup tomcat itself.
If thisn't an option then have a look at the pja toolkit.
0
 
kennethxuCommented:
gagaliya, since you are already using jdk1.4, pja can give you nothing extra than java.awt.headless. It too request change system property or access to startup script. Hope this help to save you little time on exploring into a fruitless option.
0
 
gagaliyaAuthor Commented:
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
0
 
gagaliyaAuthor Commented:
nevermind, dont have permission to install the rpm. /bangs head  

back to trying set headless mode.
0
 
kennethxuCommented:
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.
0
 
objectsCommented:
> 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.
0
 
gagaliyaAuthor Commented:
giving up on this for now, when i have more time will try more or maybe switch hosting. thanks all
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now