-2

I have Oracle JDK 17 installed on my system at the default location in my Windows 10 OS. I can use the javac compiler from the command line.

I am going to install GraalVM JDK [java17] in my system (zip file) at a different location.

Online docs have instructed me to add the path to GraalVM bin directory (Eg C:\path\graalvm\bin) to the PATH variable on Windows .

Now when i type C:\javac mySourceFile.java and press enter

How will the OS decide which java compiler(oracle javac /GraalVM javac) will it use to compile the source code as both of them are named javac ?

Here i am not explicitly passing the location of the javac to be used.

is there any way to specify which jdk to use at command line?

Please note that compiling is done using command line tools only and no IDE is used. (I am aware that IDE's allow you to select the JDK based on the location using preferences/settings)

Rahul.In
  • 61
  • 9

1 Answers1

0

When you type "javac MyClass.java", the path list defined in the PATH variable will be searched from left to right. When it finds a match, it will use that command. So, it's possible you may have include multiple java\bin folders in the path. It will execute the first one it finds. If you want a particular version of Java to execute, add it as the first path to the path variable.

Cheng Thao
  • 1,467
  • 1
  • 3
  • 9
  • is there any way to specify which jdk to use at command line? – Rahul.In Feb 02 '22 at 05:51
  • 2
    @Rahul sure, use the absolute path together with the command `C:\path\graalvm\bin\javac.exe` – user16320675 Feb 02 '22 at 12:52
  • You could write a script that executes a specific java command. For example, your script is java7.bat. In that script, execute jdk7/bin/java.exe. Make a bin folder in your home directory and add it to the path. Whenever you want to compile your code with java 7, simply java7 myclass.java. Of course, your java7.bat must takes arguments. I don't do cmd scripting so i can't give you the details. – Cheng Thao Feb 02 '22 at 16:10