• Post Reply Bookmark Topic Watch Topic
  • New Topic

UnsupportedClassVersionError exception?  RSS feed

 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading the Head First Java book, and when I got to the 12th chapter there was an animation code that was supposed to show an oval moving across a JPanel. However, when I typed the code in it compiled but threw an exception when I tried to run it. I thought I maybe missed something out when typing and went to the site that has the original source code and just pasted it in and it was the same result. Anyone knows what the problem is?
Here's the code:


And here's the output:
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked fine when I compiled and ran it.
So, maybe you have some old binaries in your classpath. Delete them all and recompile.

By the way, please post text not images.
PostTextNotScreenshots (read this)
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't able to run it, just compile it. Running it gives an exception.
I put the .java file in an empty folder and recompiled and it was still the same problem.
Sorry about the screenshot, I didn't know how to do the copying from Console2 but I got it now.

Exception in thread "main" java.lang.UnsupportedClassVersionError: SimpleAnimation :
Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(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.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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are compiling using one version of Java and executing using an earlier version.
Do:
javac -version
AND:
java -version

That should tell you at least what the difference is.
Then you'll probably need to hunt in your PATH to find out why it's picking two different ones.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have classpath variable set to anything?

Execute echo %classpath% and post the result.
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output: %classpath%
I guess that means it's empty, eh?
Was I supposed to set that? Because all of the programs before this one worked perfectly.
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:You are compiling using one version of Java and executing using an earlier version.
Do:
javac -version
AND:
java -version

That should tell you at least what the difference is.
Then you'll probably need to hunt in your PATH to find out why it's picking two different ones.


javac version: javac 1.8.0_11
java version: "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode, sharing)
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marko Matosevic wrote:Was I supposed to set that? Because all of the programs before this one worked perfectly.

No, do not set it. You don't need it. I requested this to ensure that you have not set it .

javac version: javac 1.8.0_11
java version: "1.7.0_67"


You can see here that you are compiling with version 1.8 but running with 1.7, thus the exception.
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
You can see here that you are compiling with version 1.8 but running with 1.7, thus the exception.

So, I need to update the JDK or the JRE?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adjust the PATH variable so that it points to the JDK's "bin" directory. Apparently it points to both JRE 7 and JDK 8, which causes the problem. Or you can uninstall JRE 7, assuming that don't need since you have JDK 8 installed.
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. There was some folder with shortcuts of java.exe and other stuff in the PATH, that was probably automatically created by installing the old JRE. I deleted it and pointed the JDK path to the correct bin folder and it worked.
Thanks for the help.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get this problem if you add the PATH to the new JDK installation later than the PATH to an old JRE installation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!