This discussion is archived
12 Replies Latest reply: Oct 26, 2007 11:20 PM by 807600 RSS

Exception in thread "main" java.lang.UnsupportedClassVersionError

807600 Newbie
Currently Being Moderated
Hi,

I'm new to Java and I've migrated from C# developing.
I'm using using both NetBeans and Eclipse to develop my java applications but I can't get them to run outside the IDE.
I will use the standard HelloWorld application to describe the problem.
Eclipse creates 2 files in the HelloWorld folder: src/HelloWorld.java and bin/HelloWorld.class
I tried from the commands prompt:
java HelloWorld.class
javaw HelloWorld.class
java HelloWorld
javaw HelloWorld

and it keeps replying with the same exception:
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld (Unsupported major.minor version 50.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

I have installed netbeans with JDK 6 update 3.
Please help me figure this out
  • 1. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    The command to run the HelloWorld class is
    java -cp . HelloWorld
    You cannot run the class if it was compiled for a newer version of java. You can check what version of java you are running it with by using the command
    java -version
    You can change the version of java you are using by either specifying the complete path to the java executable on the command line, or changing your shell's path so that the desired java version comes at the start.

    Once you know the version of java you are using at the command line (and have set things up so that it's the version you want) then go into your IDE and make sure you are compiling for that version. Both IDEs will document how to do this.

    Alternatively compile from the command line with
    javac -cp . HelloWorld.java
    Again, you can use -version to check the version you are using.
  • 2. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    The problem is that you are trying to run your application using an earlier version of java than
    the one which you used for compiling the files. To fix this visit the www.java.com/getjava/ to update your jre
    or compile the source with javac -source option

    cheers

    wow pbrock you got that in before i finished my little blurb, that'll work too :)

    Edited by: Octaclot on Oct 27, 2007 6:18 AM
  • 3. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    The reply for java -version is:

    java version "1.3.1_01"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
    Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

    and javac command doesn't work:

    'javac' is not recognized as an internal or external command,
    operable program or batch file.
  • 4. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    I managed to change the compilation version in Eclipse (switched it to 1.3) but how can I build in 6.0? I mean... what must I download to upgrade the compiler to run with 6.0?
  • 5. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    Learn the basics it seems your classpath is not set

    check this link the java tutorial

    http://java.sun.com/docs/books/tutorial/

    getting started pages will teach you how to set the classpath in any system
  • 6. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    I think this is a problem with the shell's path, not Java's classpath.

    Described and answered as the first common problem here:
    http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html

    (If you can't follow that, it might pay to say what operating system you are using.)

    [Edit] Also - the IDE is using 6.0. The problem lies with the version of java you are accessing from the command line. It should be 6.0 as well (most likely - 1.3 is rather old). You will have the newer version already on your system, it's just a matter of telling the shell where that version resides.
  • 7. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    I'm using Win XP SP2 and all the java software has been installed in the default folder. You are right of course, the command line uses 1.3 but how do I redirect it to 6.0? I' not very familiar with Java. Can you help me?
  • 8. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    Have you read the link that I posted?

    Using WinXPSP2 - and logging in as an administrator - I can right click on "My Computer" and select Properties. In the "System Properties" dialog I go to the "Advanced" tab and click on the "Environment Variables" button. This bring up another dialog and in the "System Variables" pane there is an entry for "Path"

    Mine says:
    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;"C:\Program Files\Symantec\Norton Ghost 2003\";C:\Program Files\Java\jdk1.6.0\bin\
    There is an "Edit" button so you can change it. The location shown above is the default (I think) but do check that it is correct for your system: ie, go into WindowsExplorer or "My Computer" and actually locate the bin folder of the java distribution. Unless you use it, you can clobber the java 1.3 entry in your path variable.

    [Edit]Perhaps it might help to explain that this PATH variable consists of a whole bunch of folders on your disk drives, separated by the ; symbol. Windows uses this list whenever you enter a command to locate the actual executable file from your dive. Your aim is to get the path to the modern Java executables (java.exe, javac.exe, jar.exe etc) onto this list. These executables are in a folder called "bin" wherever you installed them. If the worst comes to the worst you can always search for javac.exe (better than java.exe): you should locate both the 1.3 and 1.6 versions.
  • 9. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    Trouble is that I have Oracle 9i installed which comes with a jre of it's own (I see that it has it's own path in the variables tab) and the java JDK i have is version 6 Update 3 and i think it should be C:\Program Files\Java\jdk1.6.0_03\bin , right?
    My question is... do I have to remove the oracle jre from the path tab or just add this?

    Edited by: winromulus on Oct 27, 2007 12:26 AM

    I removed the oracle 1.3 jdk from path and now it displays the correct version
  • 10. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    Sorry - I don't know what effect removing 1.3 (or putting it at the end of the path after 1.6) will have on Oracle. Others may know about that.

    Like I said about you can always specify the full path on the command line. It's just a bit of a chore to have to type "C:\Program Files\Java\jdk1.6.0_03\bin\javac.exe" -cp . Foo.java all the time!
    I removed the oracle 1.3 jdk from path and now it displays the correct version
    What about running the classes you compiled with the IDE?
  • 11. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    Done... Added the 1.6 path but not at the end of the PATH string but first, before the 1.3 path. Java -version displays the correct version, and oracle works perfect

    But I'm still having one problem:

    When I use:
    javaw C:\Applications\HelloWorld\dist\HelloWorld.jar
    I get a Java Virtual Machine error: Could not find the main class. Program will exit.

    Could you help please?
  • 12. Re: Exception in thread "main" java.lang.UnsupportedClassVersionError
    807600 Newbie
    Currently Being Moderated
    make sure the HelloWorld.jar file has the correct manifest information
    if should have a directory in it called META-INF and a file in that directory called MANIFEST.MF

    MANIFEST.MF should look something like this
    Main-class: HelloWorld
    hope that helps