I have a MyFirstProgram.java file that contains two classes. I just modified the Path and tried to test the file with java
andjavac
.
public class MyFirstProgram{
public static void main(String[] args){
System.out.println("Hello ! World :)");
}
}
However, it returns me:
C:\Users\antoi\Documents\Programming\Julien>java MyFirstProgram.java
Error: Could not find or load main class MyFirstProgram.java
C:\Users\antoi\Documents\Programming\Julien>javac MyFirstProgram.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
Here is the Path:
It only works when I call javac directly in the folder:
C:\Users\antoi\Documents\Programming\Julien>"C:\Program Files\Java\jdk-11.0.2\bin\javac" MyFirstProgram.java
C:\Users\antoi\Documents\Programming\Julien>java MyFirstProgram
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: MyFirstProgram 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)
Yet it can't display the message java
and reveals there is an UnsupportedClassVersionError
. I understand from other posts it is my Java Runtime Environnment for javac
that might not be in accordance to the java
command. What steps to follow so that I can print the Hello World message ?
So, the problem is that you're compiling with javac
from JDK 11, and then trying to run with Java 8. Since the java.exe from %JAVA_HOME%\bin
is being picked up before C:\Program Files\Java\jdk-11.0.2\bin
. And, I'm guessing it can't find javac
because you added C:\Program Files\Java\jdk-11.0.2\bin
to the path, without restarting your shell, which means the path is not reloaded.
To resolve this you should remove the C:\Program Files\Java\jdk-11.0.2\bin
entry from the path, and then update the JAVA_HOME
environment variable to point to C:\Program Files\Java\jdk-11.0.2
instead. Then restart your shell and it should pick up C:\Program Files\Java\jdk-11.0.2\bin
expanded from %JAVA_HOME%\bin
.