1

I am learning Java and trying some code on Thread. I made a program using Thread class and it is compiling fine, but when I run the program it is giving the error. The complete error is:

Error: A JNI error has occurred, please check your installation and
try again

Exception in thread "main"
java.lang.UnsupportedClassVersionError: Main has been compiled by a
more recent version of the Java Runtime (class file version 53.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)

My thread java file is:

  import java.lang.Thread;

    class MyThread extends Thread
    {
        private String s;

        public MyThread(String s)
        {
            this.s = s;
        }

        public void run()
        {
            System.out.println(s);
        }
    }

And my java file with main function is: import java.util.Scanner;

class Main
{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1, s2;
        System.out.print("Enter string for first thread: ");
        s1 = scanner.nextLine();
        System.out.print("Enter string for second thread: ");
        s2 = scanner.nextLine();
        MyThread t1 = new MyThread(s1);
        MyThread t2 = new MyThread(s2);
        t1.start();
        t2.start();
        scanner.close();
    }
}

My OS is Windows 10 Home. I compiled the file using javac Main.java and tried running the compiled .class file with java Main. What is the reason of the error? How can I prevent it?

1
  • I'm sure this is a duplicate but can't find one on SO with a simple answer. Commented Jul 23, 2019 at 5:30

1 Answer 1

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

You compiled your code using Java 9 but are trying to run it on a Java 8 JVM.

4
  • How do I update the JVM? I had downloaded the .exe for updating Java, but it did not update JVM. do I need separate setup for that? Commented Jul 23, 2019 at 5:30
  • You can have multiple versions of the JDK and JVM installed. You have to set the correct JAVA_HOME when running the code. Commented Jul 23, 2019 at 5:31
  • My JAVA_HOME is set to jdk 9.0.4 Commented Jul 23, 2019 at 5:33
  • actually, the problem was that I have jre 1.8 installed whose path is in Path environment variable. so on running java Main, powershell was using java.exe in the jre instead of the jdk. I removed the jre path, now it is running Commented Jul 23, 2019 at 6:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.