java 访问 Tomcat 上的 GraphicsEnvironment.getLocalGraphicsEnvironment 时出现 NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/951948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoClassDefFoundError while accessing GraphicsEnvironment.getLocalGraphicsEnvironment on Tomcat
提问by Red33mer
I have an application which is running on tomcat, one of the methods is, creating a simple thumbnail from an jpeg image. The functions works fine offline and a week ago also on tomcat. But now i get the following error:
我有一个在 tomcat 上运行的应用程序,其中一种方法是从 jpeg 图像创建一个简单的缩略图。这些功能可以在离线和一周前在 tomcat 上正常运行。但现在我收到以下错误:
java.lang.NoClassDefFoundError
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:164)
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68)
java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1141)
eval.impl.ImageEval.getThumbnail(ImageEval.java:155)
eval.impl.ImageServlet.doGet(ImageServlet.java:79)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
I don't think that i have change anything what should influence this (actually i didn't change the function at all according to the svn repository), so it must be a library problem. But i can't figure out what is missing. Here are the actual lines from the getThumbnail function, where the error occures:
我认为我没有改变任何应该影响它的东西(实际上我根本没有根据 svn 存储库改变函数),所以它一定是一个库问题。但我无法弄清楚缺少什么。以下是 getThumbnail 函数中发生错误的实际行:
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(simage, 0, 0, thumbWidth, thumbHeight, null);
[edit] I decided to update the problem description a little. Yes it seems that he can not find some class from java.awt or one related to that. But they do exist on the server in the jvm. Java headless mode doesn't solve the problem. In another project the exact same code, but inside an axis2 webservice on this server is working fine. [/edit]
[编辑] 我决定稍微更新问题描述。是的,他似乎无法从 java.awt 中找到某个类或与之相关的类。但是它们确实存在于服务器上的 jvm 中。Java 无头模式不能解决问题。在另一个项目中,代码完全相同,但在此服务器上的axis2 webservice 中工作正常。[/编辑]
回答by OscarRyz
It seems like you've change the configuration of Tomcat.
看来您已经更改了 Tomcat 的配置。
Either you've changed to a l{0,1}[iu]n[iu]x box or installed on a virtual machine with different security control than the one where you test it.
要么您已更改为 al{0,1}[iu]n[iu]x 框,要么安装在安全控制与您测试的虚拟机不同的虚拟机上。
Apparently the
显然
GraphicsEnvironment.getLocalGraphicsEnvironment()
Is trying to access the property: java.awt.graphicsenv
正在尝试访问属性:java.awt.graphicsenv
Which may return null or some non existing class name which is then loaded and throws the ClassNotFoundException. 1
这可能返回 null 或一些不存在的类名,然后加载并抛出 ClassNotFoundException。1
The solution seems to be specifying the "java.awt.headless" property.
解决方案似乎是指定“java.awt.headless”属性。
This is a similar question: java.awt.Color error
这是一个类似的问题:java.awt.Color 错误
Try this search, it shows similar situations as your.
试试这个搜索,它显示了与你类似的情况。
I remember there was something in the sun bugs database too.
我记得在 sun bugs 数据库中也有一些东西。
Post the solution when you find it!
找到后发布解决方案!
EDIT
编辑
It is not eclipse!!
不是日食!!
In my original post there is a link to the source code of the class which is throwing the exception.
在我的原始帖子中,有一个指向引发异常的类的源代码的链接。
Since I looks like you miss it, I'll post it here for you:
因为我看起来你很想念它,我把它贴在这里给你:
public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() {
if (localEnv == null) {
// Y O U R E R R O R O R I G I N A T E S H E R E !!!
String nm = (String) java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction
("java.awt.graphicsenv", null));
try {
// long t0 = System.currentTimeMillis();
localEnv =
(GraphicsEnvironment) Class.forName(nm).newInstance();
// long t1 = System.currentTimeMillis();
// System.out.println("GE creation took " + (t1-t0)+ "ms.");
if (isHeadless()) {
localEnv = new HeadlessGraphicsEnvironment(localEnv);
}
} catch (ClassNotFoundException e) {
throw new Error("Could not find class: "+nm);
} catch (InstantiationException e) {
throw new Error("Could not instantiate Graphics Environment: "
+ nm);
} catch (IllegalAccessException e) {
throw new Error ("Could not access Graphics Environment: "
+ nm);
}
}
return localEnv;
}
That's what gets executed.
这就是被执行的。
And in the original post which you don't seem to have read, I said the code is accessing the property "java.awt.graphicsenv"
在您似乎没有读过的原始帖子中,我说代码正在访问属性“java.awt.graphicsenv”
If that other project using axis doesn't have the same problem it may be because it may be running in a different tomcat configuration or the axis library allowed the access to that property. But we cannot be sure. That's pure speculation. So why don't you test the following and see what gets printed:
如果使用轴的其他项目没有相同的问题,则可能是因为它可能在不同的 tomcat 配置中运行,或者轴库允许访问该属性。但我们不能确定。那纯属猜测。那么为什么不测试以下内容并查看打印的内容:
String nm = (String) java.security.AccessController.doPrivileged
(new sun.security.action.GetPropertyAction
("java.awt.graphicsenv", null));
System.out.println("java.awt.graphicsenv = " + nm );
It it prints null then you now what the problem is. You don't have that property in your system, or the security forbids you do use it.
它打印 null 那么你现在问题是什么。您的系统中没有该属性,或者安全性禁止您使用它。
It is very hard to tell you from here: "Go and edit file xyz and add : fail = false" So you have to do your work and try to figure out what's the real reason.
从这里很难告诉你:“去编辑文件 xyz 并添加:fail = false”所以你必须做你的工作并试图找出真正的原因。
Start by researching what's the code being executed is ( which I have just posted ) and follow by understand what it does and how does all that "AccessController.doPrivileged" works. (You may use Google + StackOverflow for that).
首先研究正在执行的代码是什么(我刚刚发布),然后了解它的作用以及“AccessController.doPrivileged”的所有工作原理。(您可以为此使用 Google + StackOverflow)。
回答by VTR Ravi Kumar
We had a similar issue and after much trouble shooting it was identified to be related to the java.awt.headlessproperty. The issue was resolved by explicitly setting the JVM option to
我们遇到了类似的问题,经过多次故障排除后,确定与该java.awt.headless物业有关。该问题已通过将 JVM 选项显式设置为
-Djava.awt.headless=true
回答by Huntrods
It wasrunning a week ago, and now it is not.
它在一周前运行,现在不是。
THEREFORE, YOU CHANGED SOMETHING BETWEEN "working" and "not working".
因此,您在“工作”和“不工作”之间改变了一些东西。
Go back to the working config (if you can), and rigorously track what you changed. If you don't have a backup of the working config, then meticulously go back through what you've done between working and non-working until you find what you changed.
返回到工作配置(如果可以),并严格跟踪您更改的内容。如果您没有工作配置的备份,那么仔细回顾一下您在工作和非工作之间所做的事情,直到找到您更改的内容。
It may not be code - it could be a config file, etc.
它可能不是代码 - 它可能是一个配置文件等。
Best of luck,
祝你好运,
-R
-R
回答by Yishai
If you are deploying this on *nix, and you don't have an X window system running anymore, that could explain it. Even if you do, if you aren't exporting the DISPLAY system variable to the process that starts the JVM, or if you are but it is not actually valid, it could cause such an issue.
如果您在 *nix 上部署它,并且您不再运行 X 窗口系统,那可以解释它。即使您这样做,如果您没有将 DISPLAY 系统变量导出到启动 JVM 的进程,或者如果您导出但它实际上无效,则可能会导致此类问题。
That would at least explain why you didn't change any configuration in tomcat, but still have a problem.
这至少可以解释为什么您没有更改 tomcat 中的任何配置,但仍然有问题。
回答by JeeBee
Is this server running java in server mode - I hear that doesn't load in the AWT classes.
这台服务器是否在服务器模式下运行 java - 我听说 AWT 类中没有加载。
回答by greenyoda
Since you're getting NoClassDefFoundError from inside the AWT code, it looks like Java is failing to load the X Windows libraries. Note that even if you're running in headless mode ($DISPLAY not pointing to an X Windows server), AWT still needs some subset of the X11 libraries in order to render images. See, for example, this reference:
由于您从 AWT 代码内部收到 NoClassDefFoundError,因此 Java 似乎无法加载 X Windows 库。请注意,即使您在无头模式下运行($DISPLAY 不指向 X Windows 服务器),AWT 仍然需要 X11 库的一些子集来渲染图像。例如,请参阅此参考资料:
http://javatechniques.com/blog/linux-x11-libraries-for-headless-mode
http://javatechniques.com/blog/linux-x11-libraries-for-headless-mode
If something stopped working and your Java code didn't change, it's possible that the X11 libraries got moved or uninstalled on your machine, or that for some other reason your LD_LIBRARY_PATH environment variable doesn't point to them anymore.
如果某些东西停止工作并且您的 Java 代码没有更改,则 X11 库可能在您的机器上被移动或卸载,或者由于某些其他原因您的 LD_LIBRARY_PATH 环境变量不再指向它们。
回答by Luke Woodward
If your NoClassDefFoundErrorhas no message at all, then this means two things:
如果您NoClassDefFoundError根本没有消息,那么这意味着两件事:
- The JVM has already tried and failed to load a class. Usually, this means the JVM was unable to complete static initialization for that class, i.e. assign values to any
staticfields and run anystatic { }blocks. Often, this is because the classes necessary to do this static initialization are missing. - You're using Java 5, not Java 6. (In Java 6, you get a 'Could not initialize class xyz' message instead.)
- JVM 已尝试加载类但未能成功。通常,这意味着 JVM 无法完成该类的静态初始化,即为任何
static字段赋值并运行任何static { }块。通常,这是因为缺少执行此静态初始化所需的类。 - 您使用的是 Java 5,而不是 Java 6。(在 Java 6 中,您会收到“无法初始化类xyz”消息。)
The problem class appears to be the one whose name is the value of the system property java.awt.graphicsenv. I would start by finding out the value of this property. What happens when you try to instantiate this class?
问题类似乎是名称为系统属性值的类java.awt.graphicsenv。我首先要找出这个财产的价值。当您尝试实例化此类时会发生什么?

