tags:

views:

528

answers:

3

I have this simple Jsp page:

<%@ page language="java" import="java.awt.Color"%> <%
Color background = Color.white;
%>

Which fails with following error:

java.lang.NoClassDefFoundError
    at _text__jsp._jspService(/text.jsp:3)
    at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
    at com.caucho.jsp.Page.subservice(Page.java:506)
    at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
    at com.caucho.server.http.Invocation.service(Invocation.java:315)
    at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
    at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:346)
    at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
    at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
    at java.lang.Thread.run(Thread.java:534)

I'm running it on Resin 2.1.13.

Any idea what's causing this?

A: 

Some VM with the -server option don't load the java.awt. package at all ( nor javax.swing and others )

This is to avoid loading classes that won't be needed.

By the way, the class

java.awt.Color

Won't be any useful in a jsp page. It is used to display colors in java desktop applications.

What are you trying to do? Perhaps there is a better way.

OscarRyz
trying to watermark a image
daniels
Oscar, in that case, it should not even possible if we do it in a POJO in the same application. So, do you mean we need another application - in order to achieve this - in another JVM?
Adeel Ansari
I had to re-read your comment several times until I get it! : - ) You're right!, the -server is used to specify some strategies for the VM to behave. The restriction is impossed by the servlet container it self, as you said. Good point.
OscarRyz
A: 

Not sure about the issue. I can run your code successfully in my Tomcat. May be this problem is particular to Resin. Or, as said by Dave, may be a headless issue.

Your best bet is to convert the image in some POJO and then spit that to the browser, or may be save it somewhere on the disk and then link it in your JSP. If problem persists, try running in headless mode, as Dave pointed out.

Moreover, its important to understand that JSP is a view technology for the web, and must not do that kind of graphics manipulation.

Adeel Ansari
Can you give a reference for the info that containers don't allow AWT to be loaded? I seem to recall that a system I once worked on used AWT classes to create an Image which was then converted to a GIF.
Dave Costa
Oops -- I misread the answer a little, you're saying it doesn't allow them in JSPs but it will allow them to be used within normal objects. Still, I would be interested to see a reference for this.
Dave Costa
No there is nothing in the specs. Actually, I can run this code in Tomcat easily, without any issues. It must be something else, may be headless thing like you mentioned. Or may be this problem in specific to Resin. Editing my post accordingly.
Adeel Ansari
+1  A: 

In the past I've used AWT classes inside servlet containers. The issue that needs to be dealt with is that, on a server system, there is probably no graphics display running that AWT can connect to, which by default causes it to fail.

The solution is to pass a system property that tells AWT it is running on a "headless" system. In general this is done by passing "-Djava.awt.headless=true" to the java command line.

Here's a reference regarding accomplishing this for Resin: http://www.caucho.com/support/resin-interest/0209/0062.html. The OP in that thread also reported a NoClassDefFound error.

Dave Costa
As far as I experienced, for headless thingy, you get some Display Error.
Adeel Ansari
+1 for correcting me.
Adeel Ansari