Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix this error: class file has wrong version 52.0, should be 49.0

Tags:

java

macos

I'm really struggling with this. I've been unable to configure Intellij to use Javac, unable to run javadoc. I just can't seem to get my java runtime environment stable and working productively on Mac OSX.

I've created the most basic of examples which is a hello world program and it still errors from command line. Everything I read about this error tells me to "upgrade the compiler". But I'm really not sure what the problem is, the javac & java are both running 1.8 (now, after I tried upgrading the system JVM).

I explicitly set the path when calling javac, checked the version etc. Everything tells me it's 1.8.

Can anyone explain how to fix this? (without reverting my system to a 1.5 SDK).

This is output from the basic hello world application:

$JAVA_HOME/bin/javac Main.java
Main.java:3: cannot access java.lang.Object
bad class file: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)
class file has wrong version 52.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
public class Main {
       ^
1 error
localhost:mycompany$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
localhost:mycompany$

Output from javac version:

javac -version
javac 1.8.0_45
javac: no source files
Usage: javac <options> <source files>

Output from java version:

java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
like image 222
Richard G Avatar asked May 06 '15 13:05

Richard G


2 Answers

The error message means that you try to load a Java 8 class with a Java 5 VM.

For an unknown reason $JAVA_HOME/bin/javac is using the JDK 8 Java runtime (.../rt.jar) but the compiler itself is loaded with a Java 5 VM.

My next step would be to make sure that $JAVA_HOME/bin/javac is actually what it should be (and not a script which does something weird). I'd also check the environment (CLASSPATH) and options like -Xbootclasspath. Try set | grep -i java and set | grep -i path to look for suspicious variables.

If nothing seems to work, try to reinstall the Java 8 SDK again.

like image 158
Aaron Digulla Avatar answered Sep 19 '22 15:09

Aaron Digulla


I had exactly the same issue with the same versions when trying to build the Arduino IDE using ant. The fix is to remove an old version of tools.jar from /Library/Java/Extensions/ - just wanted to clarify for others that hit the same issue.

like image 26
B. Burn Avatar answered Sep 17 '22 15:09

B. Burn