Solved

dynamically generate images on serverside

Posted on 2004-03-22
14
1,300 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:

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


0
Comment
Question by:gagaliya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 10647494
> 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
 
LVL 7

Expert Comment

by:searlas
ID: 10647850

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
 
LVL 1

Author Comment

by:gagaliya
ID: 10651398
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
Independent Software Vendors: 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!

 
LVL 1

Author Comment

by:gagaliya
ID: 10652071
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
 
LVL 7

Assisted Solution

by:searlas
searlas earned 100 total points
ID: 10653224
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
 
LVL 7

Expert Comment

by:searlas
ID: 10653337

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
 
LVL 14

Expert Comment

by:kennethxu
ID: 10655166
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
 
LVL 92

Expert Comment

by:objects
ID: 10655272
> how do i do this?

When you startup tomcat itself.
If thisn't an option then have a look at the pja toolkit.
0
 
LVL 14

Assisted Solution

by:kennethxu
kennethxu earned 100 total points
ID: 10657735
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
 
LVL 1

Author Comment

by:gagaliya
ID: 10662962
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
 
LVL 1

Author Comment

by:gagaliya
ID: 10662992
nevermind, dont have permission to install the rpm. /bangs head  

back to trying set headless mode.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10663772
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
 
LVL 92

Expert Comment

by:objects
ID: 10663785
> 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
 
LVL 1

Author Comment

by:gagaliya
ID: 10706879
giving up on this for now, when i have more time will try more or maybe switch hosting. thanks all
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft is moving in-place eDiscovery & hold from ECP to EOP console under Content Search in Search and Investigation Options.  In this post, I will be showing you how to export emails to a PST file using the Content Search Options.
In this blog we highlight approaches to managed security as a service.  We also look into ConnectWise’s value in aiding MSPs’ security management and indicate why critical alerting is a necessary integration.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question