0

Hi I am new to Java I am trying to get started in it but when I try to run my hello world script I run into this error. I have pumped this error into google and come back with a 7 year old stack overflow answer talking about versions that are compatible and what not. So I tried uninstalling and going back a version but then apparently I have to sign in and provide a company name in case of billing? I am not sure but either way I can't do that. So I reinstalled all of Java. This is what I have from a java-version:

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)

After that I deleted the file I had compiled before and tried again. It seems to compile the file no issue but then when I try to run it I still get.

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: MyClass 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)

If it makes a difference this is what my hello world statement looks like.

public class MyClass {
  public static void main(String args[]) {
    System.out.println("Hello World");
  }
}

I did it just how the tutorial showed so I don't think that the issue is there but still im a noob so any help is greatly appreciated.

Simson
  • 3,373
  • 2
  • 24
  • 38

1 Answers1

1

The problem is, that your Java runtime is version 8 and your compiler is version 13, hence the incompatibility. If you run javac -version it will tell you something like javac 13.0.1. You should check that you uninstall Java 8 and use only the runtime bundled with your JDK. Then the version incompatibility should be gone.

Alternatively you could add --release 8 to your compiler invocation. This will tell the Java 13 compiler to produce bytecode which is compatible with Java 8.

For example consider the following class:

public class Main{
    public static void main(String[] args) {
        System.out.println("Hello world!");
        System.out.println("Running on Java version " 
           + System.getProperty("java.version"));
    }
}

Then compile it without additional flags:

> jdk13/bin/javac .\Main.java

and run it on Java 13:

> jdk13/bin/java Main
Hello world!
Running on Java version 13.0.1

and on Java 8:

> jdk8/bin/java Main
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 57.0), this version of the Java Runtime only
recognizes class file versions up to 52.0
...

Which crashes as expected.

Now compile it with the release flag:

> jdk13/bin/javac --release 8 .\Main.java

And test again:

> jdk8/bin/java Main
Hello world!
Running on Java version 1.8.0_192

> jdk13/bin/java Main
Hello world!
Running on Java version 13.0.1

Everything works fine.

Jannik
  • 1,583
  • 1
  • 14
  • 22
  • Using that release command worked for me but uninstalling and compiling without the older version of runtime installed still gave me an error. I think I would rather compile with the older version though so I will just continue to use that. Thank you for the help. – sam.solo.works Oct 27 '19 at 12:23
  • But when you've uninstalled the old version it should at least give a different error message. Otherwise the old Java version is still on your system. – Jannik Oct 27 '19 at 12:51
  • It does compile it without the older version of runtime but it would not run it saying that there is no runtime at all. I just uninstalled the JDK and reinstalled it as well to make sure there was no components missing from it. but it still is telling me the runtime is missing. I looked for a runtime 13.0.1 but it just brings up the JDK again. You said its supposed to have its own runtime right? – sam.solo.works Oct 27 '19 at 15:08
  • Yes, normally the JDK contains a Java runtime environment. You should find the `java.exe` or just `java` in your `bin` directory of the JDK. It is possible, that the JRE is not added to your `path` environment variable, so the command line does not find the JRE. – Jannik Oct 27 '19 at 15:47
  • Yes you were correct and that was not added. I went into the bin folder and found java.exe and added it to the path but still the error says the same error Error: opening registry key 'Software\JavaSoft\Java Runtime Environment' Error: could not find java.dll Error: Could not find Java SE Runtime Environment. Sorry I cant get this comment section to add these portions as code – sam.solo.works Oct 27 '19 at 16:41
  • Is this error generated by Eclipse or by the JRE? If it is generated by Eclipse it might help to create a `JAVA_HOME` environment variable. The content should be the path to the root directory of your JDK. By the way, you can add code in comments if you enclose it with `backticks`. – Jannik Oct 27 '19 at 17:07
  • oh `like this?` – sam.solo.works Nov 02 '19 at 15:33
  • Ah! thanks for that. No the error is coming from command line I am writing all the code in notepad plus and running it from command line. I googled where JRE is in the JDK package and found a forum where they said they stopped including it after JDK 11 so is that possibly what the case is im running into now you think? – sam.solo.works Nov 02 '19 at 15:34
  • I don't think that is the problem. But the the uninstall probably didn't clean up everything. Check out this solution: https://superuser.com/a/1382163 – Jannik Nov 02 '19 at 21:41