01.JAVA/Java2008. 8. 28. 10:58
반응형
How do I resolve a java.lang.UnsupportedClassVersionError?
Abstract: How do I resolve a java.lang.UnsupportedClassVersionError?

Issue:

When I attempt to run my Servlet, I receive the following exceptions:

javax.servlet.ServletException: Error allocating a servlet instance
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:185)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:653)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:534)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.doTask(ProcessorTask.java:403)
com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:55)

root cause java.lang.UnsupportedClassVersionError: untitled1/Servlet1 (Unsupported major.minor version 49.0)
java.lang.ClassLoader.defineClass0(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:537)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1774)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:905)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1370)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1234)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:185)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:653)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:534)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.doTask(ProcessorTask.java:403)
com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:55)

How do I resolve this issue?

Solution:

This exception can occur when the source is built targeting a JDK that is not supported by the JDK attempting to run it. In the above example, if the servlet was built targeting only JDK 1.5, but the web server runs JDK 1.4, the above exception will occur.

Check, Project Properties | Build | Java | Target VM, and verify that the Target VM indicates a JDK compatibility that is appropriate for the JDK that is running it. It is recommended to set this field to 'All Java SDKs' for the most compatibility. The only reason you would want to specify a particular version is if your code uses language features that only appear in a particular verison of the JDK, and you want to ensure that users use only a compatible JDK.

Also check, Project Properties | Build | Java | Language Features, and verify that the Language Features are applicable with JDK that is running it.

--------------------------------------------------------------------------------------------------

java.lang
Class UnsupportedClassVersionError

java.lang.Object
  extended byjava.lang.Throwable
      extended byjava.lang.Error
          extended byjava.lang.LinkageError
              extended byjava.lang.ClassFormatError
                  extended byjava.lang.UnsupportedClassVersionError
All Implemented Interfaces:
Serializable

public class UnsupportedClassVersionError
extends ClassFormatError

Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.

Since:
1.2
See Also:
Serialized Form

Constructor Summary
UnsupportedClassVersionError()
          Constructs a UnsupportedClassVersionError with no detail message.
UnsupportedClassVersionError(String s)
          Constructs a UnsupportedClassVersionError with the specified detail message.
 
Methods inherited from class java.lang.Throwable
fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UnsupportedClassVersionError

public UnsupportedClassVersionError()
Constructs a UnsupportedClassVersionError with no detail message.

UnsupportedClassVersionError

public UnsupportedClassVersionError(String s)
Constructs a UnsupportedClassVersionError with the specified detail message.
Parameters:
s - the detail message.

JavaTM 2 Platform
Std. Ed. v1.4.2

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright 2003 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
-------------------------------------------------------------------------

java.lang.UnsupportedClassVersionError

By Alvin J. Alexander, devdaily.com

You know what will really screw with your mind? When there is a 1.4.2 version of a java.exe file in the C:\Windows\System32 directory of your Windows XP system, and you're trying to compile and run a Java 1.5 program from the command line. I kept getting this error message and couldn't figure it out, even though I knew what it meant(!):

C:\Al\JavaProjects\AutomatedGUITester\deploy>java -jar xylocator.jar

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/devdaily/
xylocator/XYLocator (Unsupported major.minor version 49.0)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

I knew that this error message was telling me that I was trying to run a Java program/class with a JVM version that was older than what I compiled it with, but I thought I had everything set up right, including my JAVA_HOME and PATH environment variables. (FWIW, after a little research it looks like the "Unsupported major.minor version 49.0" message refers to Java 1.5.0.)

Now, before you think I'm too crazy I compile my program with this batch file:

REM compile.bat
set JAVA_HOME=C:\jdk1.5.0_06
set PATH=C:\Windows;C:\Windows\System32;C:\jdk1.5.0_06\bin
set CLASSPATH=

javac com/devdaily/xylocator/XYLocator.java

Then I would create my jar file with this batch script:

REM createjar.bat
set JAVA_HOME=C:\jdk1.5.0_06
set PATH=C:\Windows;C:\Windows\System32;C:\jdk1.5.0_06\bin
set CLASSPATH=

jar cfm xylocator.jar Manifest.txt com/devdaily/xylocator/*.class

And finally, I would try to run it like this:

REM runjar.bat (BAD VERSION, NEED TO FIX PATH)

set JAVA_HOME=C:\jdk1.5.0_06
set PATH=C:\Windows;C:\Windows\System32;C:\jdk1.5.0_06\bin
set CLASSPATH=
set QTJAVA=

java -jar xylocator.jar

Now, once I realized that the #%$! java.exe file was in the C:\Windows\System32 directory the problem was easy to fix. I just had to put the Java 1.5 directory in the PATH before the System32 directory, like this:

REM runjar.bat
set JAVA_HOME=C:\jdk1.5.0_06
REM This works because the JDK is in the PATH before the System32
REM directory.
set PATH=C:\jdk1.5.0_06\bin;C:\Windows;C:\Windows\System32
set CLASSPATH=
set QTJAVA=

java -jar xylocator.jar

Now it works like a champ. FWIW, this Java program lets me determine the x/y coordinates of any point I choose on the screen. I basically take a snapshot of the screen, then display that snapshot as an overlay to the entire desktop, then use mouse click information to get the x/y coordinates. Some of this had to be done using Java 1.5.x, or native code, which I don't want to get into because I want this to work on the Mac as well as Windows.

------------------------------------------------------------------------
Posted by 1010