5

I am not a Java programmer really, so I am posting this question. The exception is being thrown java.lang.UnsupportedClassVersionError in my main class in an eclipse project. If I comment out the imports that this class has, it compiles and runs fine. If I put the imports back in, it does not work. Does this mean that the libraries I am importing were compiled with a newer or older version of java than I have? when i do java -version on the system i get 1.5_07

I could've sworn this was actually working last week, but maybe some setting in eclipse got tweaked? Is the Java Build Path in eclipse what I need to look for to check the JRE and compiler versions?

Derek
  • 11,715
  • 32
  • 127
  • 228

6 Answers6

21

In Eclipse, the compiler version is set independent of the JRE version. That means you can set the compiler to Java version 1.6 and use the JRE 1.5. In this case, you compiled classes cannot be run.

You can check the preferences in Window / Preferences / Java / Compiler for the default compiler compliance level, or the properties of your project for a project specific compiler compliance level. Compare that level with the JRE used in your project (Project / Properties / Java Build Path -> Libraries / JRE System Library) and in your program's launch configuration (via the Run / Run Configurations... menu).

Christian Semrau
  • 8,913
  • 2
  • 32
  • 39
  • This was hte problem. Thanks. I hadn't used eclipse much and didnt realize that it could be set independently like that – Derek May 26 '10 at 16:19
2

UnsupportedClassVersionError means that the Java runtime environment you are using doesn't recognise the version of a class file that you are trying to execute. The most common cause for this error is trying to use a class file compiled for a newer Java version on an older Java version - for example, you are trying to use a class compiled for Java 6 on a Java 5 runtime environment.

As Eugene explained, Eclipse has its own built-in compiler, it does not use the compiler from the JDK - so that's how you can end up with Java 6 class files even if you're running on Java 5.

Christian explains how to set the Java class file version in Eclipse.

Jesper
  • 202,709
  • 46
  • 318
  • 350
1

Eclipse is not using JDK compiler, but has its own compiler which can produce bytecode for any JRE. You need to make sure that compiler settings in your project are set to the same or lower version as the JRE you have registered in Eclipse. See Window / Preferences / Java / Installed JREs and also check what JRE is used in your launch configuration (see Run menu for that).

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
0

If you are using Maven with Eclipse, you can use the Maven plugin to update the project and then do a Maven "clean" . This resolved a goofy and unexpected show of this error in my case.

iowatiger08
  • 1,892
  • 24
  • 30
0

If your imported classes come from an external library (i.e., not compiled from sources within your project), then you should validate that the JRE used for your project is new enough for this library.

Christian Semrau
  • 8,913
  • 2
  • 32
  • 39
0

I know this is an OLD thread now but I had a recent adventure with this type of error.

When trying to compile my project within Eclipse using an Ant build file.

  • Eclipse Kepler (Java 1.7)
  • Ant 1.7
  • Project = Java 1.5

All internal paths and compiler settings were amended to look at Java 1.5 and the project built fine in Eclipse.

When trying to compile using Ant I got the java.lang.UnsupportedClassVersionError in eclipse and began looking around - including on here and finding this page.

My resolution was this :

Windows > Preferences > Ant > Runtime > Global Entries

Global Entries was pointing at the Java 1.7 tools.jar

I added a new External Jar (on the right menu) and pointed it at the Java 1.5 tools.jar

I removed the original entry (for 1.7 tools.jar) and my ant builds started working.

thonnor
  • 1,206
  • 2
  • 14
  • 28