-2

I have installed JDK 5, JDK 6 and JDK 7 on my Windows PC. I have added the binary folders of all three versions in the PATH variable. I can use different versions for different projects with IDEs. But, how do I use a specific version of javac in cmd or PowerShell?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

3 Answers3

5

cmd will pick the first JDK in your path. You only want one there.

To use different ones you can use path variables like JAVA_HOME and change it when you need to. Or do it like Bhavik Ambani specified. You will have to restart the cmd after you change your path for it to pick it up.

Sully
  • 14,672
  • 5
  • 54
  • 79
5

For that you should write the explicit path of the javac location.

E.g. (for PowerShell)

& "H:\Program Files\Java\jdk1.7.0\bin\javac.exe" JavaFile

E.g. (for cmd)

"H:\Program Files\Java\jdk1.7.0\bin\javac.exe" JavaFile
SpellingD
  • 2,533
  • 1
  • 17
  • 13
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
2

cmd will pick the first binary it finds in PATH, so depending on the order of your Java directories javac from JDK 5, 6 or 7 will be called when you type javac.

You can create links to different versions of javac and name them e.g. javac5, javac6 and javac7 and use these from the command line instead.

If you're calling javac from a build system, a makefile or a script you can use full paths instead.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92