We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

dynamically generate images on serverside

gagaliya
gagaliya asked
on
Medium Priority
1,335 Views
Last Modified: 2011-10-03
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/Web/Web_Languages/JSP/Q_20907938.html


Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:

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.

Author

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!

Author

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

Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:

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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> how do i do this?

When you startup tomcat itself.
If thisn't an option then have a look at the pja toolkit.
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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

Author

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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.

Author

Commented:
giving up on this for now, when i have more time will try more or maybe switch hosting. thanks all
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.