1

I am trying to run this simple HelloWorld code:

public class HelloWorld{

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

}

It compiles just fine but when I try to run it I get the following error:

enter image description here

I believe I have the most up to date jdk and jre. I'm brand new at java and would appreciate any help.

Brett
  • 67
  • 8
  • Your `java` version is 1.8 while your `javac` version is 12. Code compiled in later versions of Java cannot be executed by earlier versions of Java. Check your environment variables. – Slaw Jul 13 '19 at 17:51
  • @slaw so you're saying i need to download the jdk 8? – Brett Jul 13 '19 at 17:56
  • You appear to already have both Java 8 (though I can't tell if it's the JDK or just the JRE) and Java 12 installed. What I'm saying is you need to check your environment variables (e.g. `JAVA_HOME`, `PATH`) to make sure they're configured correctly (i.e. everything is pointing to the same version). What does your `PATH` look like? – Slaw Jul 13 '19 at 18:06
  • @Slaw This is my path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\dotnet\;C:\Program Files\Tesseract-OCR;C:\Program Files\Java\jdk-12.0.1\bin – Brett Jul 13 '19 at 18:10
  • The first entry, `...\Oracle\Java\javapath`, is likely pointing (indirectly) to Java 8 and was likely added when you installed Java 8. This entry doesn't include tools like `javac`, however, so those gets picked up by your last entry: `...\jdk-12.0.1\bin`. – Slaw Jul 13 '19 at 18:14

2 Answers2

3

From the Windows command prompt image you posted, it looks like you are on Windows 7.
The where command will show the path of files java.exe and javac.exe.
Enter the following command at the command prompt:

where java.exe

On my Windows 10 machine with JDK 12.0.1 installed, the output for the above command is:

C:\Program Files\Java\jdk-12.0.1\bin\java.exe

And for javac.exe ...

where javac.exe
C:\Program Files\Java\jdk-12.0.1\bin\java.exe

The folder for javac.exe must be included in the PATH environment variable, otherwise the where command would not find it.

The folder for java.exe need not be included in PATH. You may have defined the JAVA_HOME environment variable, but even that is not required.

In any case, as others have stated, a lower version of the JDK cannot run code that is compiled with a higher version (although the opposite is allowed, i.e. you can compile with a lower version and run it with a higher version).

It would appear that you have more than one JDK (or JRE) version installed. You need to decide which you want to use and modify the PATH environment variable so that it includes the desired folder containing file javac.exe.

If you don't require several java versions installed, I recommend simply removing all those that you don't require.

Abra
  • 19,142
  • 7
  • 29
  • 41
2

Yeah, seen an issue like this, it's because of the jdk version you are using to compile your code is not the same with the jdk you are running on your IDE (most likely Eclipse)

You can change the setting in: Project properties --> Java build path --> Libraries --> select preferred JRE compiler