5

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:

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:

        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]

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
Red33mer
  • 810
  • 3
  • 15
  • 22

7 Answers7

7

It seems like you've change the configuration of 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.

Apparently the

 GraphicsEnvironment.getLocalGraphicsEnvironment()

Is trying to access the property: java.awt.graphicsenv

Which may return null or some non existing class name which is then loaded and throws the ClassNotFoundException. 1

The solution seems to be specifying the "java.awt.headless" property.

This is a similar question: java.awt.Color error

Try this search , it shows similar situations as your.

I remember there was something in the sun bugs database too.

Post the solution when you find it!

1.GraphicsEnvironment.java

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"

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:

        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.

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.

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).

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • No, that's not it. In fact i forgot to mention this, but it isn't really important, in another project where a webservice is deployied with axis2 this function does work, on the same tomcat 5.5 server. I still tried the headless mode, it didn't change anything. The tomcat is the same as a week ago, i can only assume that something in eclipse (not the code) has changed, because there is no other possible expaination, but still i can't figure out a solution. – Red33mer Jun 05 '09 at 05:49
  • That would throw an Error, not a NoClassDefFoundError, which is a subclass. And it would report the name of the class it was looking for. – Yishai Jun 05 '09 at 19:14
  • @Yishai: Except if NoClassDefFoundError is thrown fist right? The catch section handles 3 Exception but not an error ( because they should not be handled in first place ) If NoClassDefFoundError is thrown it will not be handled in the try/catch and will pop up in the stacktrace ( just as reported by Red33mer ) NoClassDefFoundError != NoClassDefFoundException. http://bit.ly/ncdfe != http://bit.ly/cnfep – OscarRyz Jun 05 '09 at 20:05
  • Thanks a lot so far. Executing the suggestion you mentioned above is printing java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment. I get it, it is failing in GraphicsEnvironment localEnv = (GraphicsEnvironment) Class.forName(nm).newInstance(); Still i don't know what know do with this knowlege or how to fix the problem. I tried to find a way around using the awt classes, but had no luck so far. – Red33mer Jun 06 '09 at 11:54
  • @Red33mer: That's odd, according to this: http://bit.ly/EPSrh and this: http://bit.ly/UzmIZ The problem should be fixed with the "java.awt.headless=true". This entry has the same solution: http://bit.ly/cSy6W – OscarRyz Jun 08 '09 at 17:17
  • The thing is, that if u look in the class, the headless feature is executed after the local graphics environment ist created. So headless option doesnt work. – Red33mer Jun 09 '09 at 10:35
  • Solved, this was a bug in tomcat 5.5, restarting fixed the problem. Since Oscar took a lot of time to figure out the problem, i will give him the solve check flag ;). Still i would like to thank all guys who took their time to help me. – Red33mer Jun 20 '09 at 08:15
5

We had a similar issue and after much trouble shooting it was identified to be related to the java.awt.headless property. The issue was resolved by explicitly setting the JVM option to

-Djava.awt.headless=true
Mat
  • 202,337
  • 40
  • 393
  • 406
3

It was running 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

Huntrods
  • 2,561
  • 3
  • 22
  • 29
  • Yeah i got that, something hast changed, still, i checked out an old version from the svn, still not working, the tomcat configuration has not been changed or even touched since then. This is actually the reason that i'am asking this question, because, i quite frankly can not explain this. – Red33mer Jun 04 '09 at 18:03
1

Is this server running java in server mode - I hear that doesn't load in the AWT classes.

JeeBee
  • 17,476
  • 5
  • 50
  • 60
  • No that's not it, a week ago it did work. The tomcat configuration has not been changed since. I think u see why this error is puzzling me. – Red33mer Jun 04 '09 at 18:05
  • Hmm, had an OS upgrade - new version of x.org or nvidia/intel/ati linux drivers? I see that Native method in the stacktrace... – JeeBee Jun 04 '09 at 23:00
  • No, i didn't had an Os upgrade. – Red33mer Jun 05 '09 at 05:50
  • Oh well. Good luck hunting down the issue! – JeeBee Jun 05 '09 at 14:29
  • @JeeBee: I remember something similar, but I don't have a reliable source. Do you remember where did you hear that? – OscarRyz Jun 05 '09 at 17:57
  • I wish I had the link to hand but I don't. I suspect it was here at some point in the past. I also suspect that it will still load the AWT classes on demand, maybe the client VM preloads them in anticipation ... I don't know and can't say for sure. – JeeBee Jun 05 '09 at 21:08
1

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.

That would at least explain why you didn't change any configuration in tomcat, but still have a problem.

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • Actually i have the exact same function in another project deployied on this tomcat, which is using axis2 instead, and it's working. But if it were a DISPLAY system variable problem, it shouldt work either. I really think that eclipse screwed something up, because i don't see any other possible explaination. – Red33mer Jun 05 '09 at 05:52
0

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:

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.

greenyoda
  • 11
  • 2
0

If your NoClassDefFoundError has no message at all, then this means two things:

  1. 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 static fields and run any static { } blocks. Often, this is because the classes necessary to do this static initialization are missing.
  2. You're using Java 5, not Java 6. (In Java 6, you get a 'Could not initialize class xyz' message instead.)

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?

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104