How To Fix Java Unsupported major.minor Version 51.0 Exception

When you run a program in Eclipse or command line. You may encounter the following exception java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0. This is because your java class compiler version is bigger than the java class runtime version. In my environment, my java runtime version is 1.6, but my eclipse java compiler version is 1.8. So I need to upgrade my eclipse java runtime JDK from version 1.6 to 1.8.

1. The Error java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0 Stack Trace.

  1. Below is the java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0 error stack trace output in my eclipse console.
    java.lang.UnsupportedClassVersionError: com/dev2qa/java/basic/CheckLeapYear : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Exception in thread "main"

2. How To Find The Java Class Used Java Runtime Version.

  1. If you use eclipse, you can open your java class source file in the eclipse code editor.
  2. Right-click anywhere in the java source code editor, then click the Run As —> Run Configurations… menu item in the popup menu list.
  3. In the Run Configurations pop-up dialog window, click the JRE tab on the right panel.
  4. Then you can find the java class used runtime JRE version in the tab content Runtime JRE: area.
  5. There are 3 choices for you to choose the runtime JRE. You can choose either Project execution environment, Execution environment from the Environments drop-down list, or Alternate JRE from the Installed JREs… drop-down list.
  6. To fix this error, you should make sure the java runtime JDK version you selected here is equal to or bigger than the eclipse project java compiler version.

3. How To Find The Eclipse Project Used Java Compiler Version.

  1. Select the eclipse project in the eclipse left side Project Explorer panel, click the Project —> Properties menu item at the eclipse top menu bar.
  2. In the popup project Properties dialog window, click the Java Compiler menu item on the left side.
  3. Check the checkbox Enable project specific settings.
  4. Uncheck the checkbox  JDK Compliance —> Use compliance from execution environment ‘JavaSe-1.8’ on the ‘Java Build Path’.
  5. Select the java compiler JDK version ( should smaller than or equal to the java runtime JDK version ) from the Compiler compliance level drop-down list.
  6. Click the OK button to save the changes.

4. How To Add Alternate JDK Version In Eclipse.

  1. Open eclipse and click Window —> Preferences menu item in the eclipse toolbar.
  2. Click Java —> Installed JREs in the left panel of the popup eclipse Preferences window.
  3. Click the Add button on the right panel, then it will popup the select JRE Type dialog.
  4. In the Installed JRE Types list, select the Standard VM item, click the Next button.
  5. Click the Directory… button after the JRE home input textbox to choose the JDK installed directory. ( not JRE installed directory )
  6. Click the Finish button in the JRE Definition dialog window to complete the JDK add process.
  7. In the Installed JREs list window, check the checkbox before the JDK you just added, then click the OK button to save the changes.

5. Video Demo For This Article.

  1. You can see the video demo for this article on my youtube channel with the link https://youtu.be/nSZyHnU1_Gs.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x