java.awt.Color error

2019-08-21 07:54发布

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?

标签: java jsp resin
4条回答
男人必须洒脱
2楼-- · 2019-08-21 08:21

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-21 08:25

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.

查看更多
祖国的老花朵
4楼-- · 2019-08-21 08:35

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.

查看更多
太酷不给撩
5楼-- · 2019-08-21 08:36

I had this same issue on Tomcat on Linux. I would get this message intermittently. It was due to maxing out the number of open File Descriptors on the OS.

I'm not sure how Java loads classes as required, but I assume this limit stopped it from loading classes that it needed from the runtime.

I followed these instructions outlined here:

How do I change the number of open files limit in Linux?

Namely:

Setting a hard limit in /etc/security/limits.conf

* hard nofile 64000

Logging out and logging in again, then running:

ulimit -n 64000

in my shell session before starting Tomcat. I added the above command to my .bashrc file to make sure that the limits were set each time I logged on.

查看更多
登录 后发表回答