java.awt.Color error

故事扮演 提交于 2019-12-25 04:34:23

问题


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?


回答1:


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.




回答2:


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:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/427482/java-awt-color-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!