0

While compiling from the command line, an error occurs, how do I get the required version of Java Runtime?

java.lang.UnsupportedClassVersionError: hello has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0

C:\Users\s\Documents\Documents>javac hello.java

C:\Users\s\Documents\Documents>java hello
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: hello has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 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)
RamPrakash
  • 1,687
  • 3
  • 20
  • 25
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50

1 Answers1

3

It looks like you're using a different JRE than you have Java compiler.

java -version

and

javac -version 

and you should see a difference in version.

Once you confirm that, you can then either fix the path issue causing the difference or set a compilation target with javac

javac -target 1.8 hello.java

as an example

Mike K.
  • 3,751
  • 28
  • 41
  • it isn't showing me java version – Sweta Jain Jan 20 '20 at 20:12
  • 1
    @SwetaJain who isn't? If the first two commands Mike gave you don't work, try `-version` instead of `--version` (the latter was introduced with java9 iirc). If that still doesn't work, download an actual java runtime, because what you're using is probably trash. – Federico klez Culloca Jan 20 '20 at 20:13
  • @FedericoklezCulloca C:\Users\s\Documents\Documents>javac --version javac 13.0.1 C:\Users\s\Documents\Documents>java --version Unrecognized option: --version Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. – Sweta Jain Jan 20 '20 at 20:14
  • 1
    @SwetaJain do the java one as `java -version` instead of -- – Mike K. Jan 20 '20 at 20:16
  • @FedericoklezCulloca ok 'java version "1.8.0_231" Java(TM) SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot(TM) Client VM (build 25.231-b11, mixed mode)' and javac 13.0.1 . What is it supposed to be ? – Sweta Jain Jan 20 '20 at 20:16
  • 1
  • @FedericoklezCulloca What exactly am I supposed to do? – Sweta Jain Jan 20 '20 at 20:27
  • 1
    Check that those versions match or at least that `javac -version` < `java -version` – Federico klez Culloca Jan 20 '20 at 20:36
  • If you have a 1.8 JRE and a Java 13 JDK it might be that you have different versions in your PATH, and/or in an order that you're not expecting. Do you need a JRE1.8 as well as JDK 13? If not the quickest answer might be to uninstall 1.8 – Kevin Hooke Jan 20 '20 at 22:00