0

Win 10, x64, jdk 1.8.0_201

I have just compiled & tried to run my first Java program,

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

I compiled via the command line using,

javac FirstProgram.java

At first Windows wasnt picking up the compiler but then I added D:\Program Files\Java\jdk1.8.0_201\bin to my PATH variable and I get a file called A.class with no compilation errors.

But when I try to run A.class by entering java A at the command line I get the following...

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: A has been compiled by a more recent version of the Java Runtime (class file version 55.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)

So I checked which version of Java I'm running by entering java -version and get...

java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

So I'm confused why doesnt this work? Aren't I using a compatable compiler even though it is the same jdk version as my runtime environment? (please no advice about using an IDE)

DrBwts
  • 3,470
  • 6
  • 38
  • 62
  • 1
    That should have worked. Are you sure you've done exactly what you've written here? For instance why are you calling `javac` on `FirstProgram` and not `a.java`? – Malt Mar 10 '19 at 16:07
  • 2
    What do you get if you run `javac -version`? – Malt Mar 10 '19 at 16:09
  • 1
    You have compiled with Java 11, not Java 8. – JB Nizet Mar 10 '19 at 16:09
  • Have you tried to reboot the cmd? Path changes aren't reflected on currently opened cmd, without reboot. – Pablo Castro Mar 10 '19 at 16:09
  • yes Malt & JB called it. I compiling with Java 11 somehow. How do I change that? – DrBwts Mar 10 '19 at 16:18
  • By using the absolute path of the javac.exe program, or by putting the correct JDK bin directory in the path. If you have two JDK bin directories in the path, fix that: the first will always be used. – JB Nizet Mar 10 '19 at 16:23
  • turns out there was a JRE path from an SPSS install above my JDK entry. Have moved it & it all works now – DrBwts Mar 10 '19 at 16:28

0 Answers0