Question
What does the error java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0 mean, and how can I resolve it?
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Answer
The error java.lang.UnsupportedClassVersionError occurs when you attempt to run a Java class that was compiled with a more recent version of Java than that which is currently being used to execute it. In this case, the error message indicates that the class was compiled with Java 7 (major version 51) but is being run with an older version of Java Runtime Environment (JRE).
javac -source 1.6 -target 1.6 HelloWorld.java
java HelloWorld
Causes
- The Java class was compiled with a newer version of JDK (e.g., JDK 7) but is being executed with an older version of JRE (e.g., JRE 6 or earlier).
- Mismatch between the JDK that compiled the class and the JRE you are using to run your application.
Solutions
- Upgrade your JRE to a version that matches or exceeds the version used to compile the Java class (in this case, JRE 7 or higher).
- If you need to stick with the older JRE version, recompile your Java code with an option to target an earlier Java version (e.g., using the -source and -target flags during compilation).
- Consider installing the JDK if you are just using the JRE; the JDK includes tools for compiling code and more environments for seamless development.
Common Mistakes
Mistake: Not updating the JRE after installing a newer JDK.
Solution: Always ensure that your JRE is at least at the version level of the JDK you are using to compile your applications.
Mistake: Confusing PATH setup between JRE and JDK.
Solution: Set your PATH variable to point to the JDK's bin directory for compiling and running Java programs.
Helpers
- java.lang.UnsupportedClassVersionError
- Unsupported major.minor version
- JDK vs JRE
- Setting PATH variable for Java
- Java compilation error