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?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Shantanu Shinde
  • 932
  • 3
  • 23
  • 48

1 Answers1

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 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? – Shantanu Shinde Jul 23 '19 at 05:30
  • You can have multiple versions of the JDK and JVM installed. You have to set the correct `JAVA_HOME` when running the code. – Jim Garrison Jul 23 '19 at 05:31
  • My `JAVA_HOME` is set to jdk 9.0.4 – Shantanu Shinde Jul 23 '19 at 05: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 – Shantanu Shinde Jul 23 '19 at 06:36