java.lang.UnsupportedClassVersionError (UnsupportedClassVersionError)

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.