Set Java path in command line for only one directory
Asked Answered
D

3

5

I am running a program that utilizes Scala 2.10 for work and is not compatible with Java 8, only Java 7. In a Windows 7 command line, how can I set the java path to use Java 7 ONLY for that directory?

Domel answered 15/6, 2015 at 21:52 Comment(1)
I don't believe that windows allows you to have per-folder environment variables. Why not make a .bat file which sets JAVA_HOME and then runs your program?Maroon
L
4

If the program uses a batch to start, then add this line before the start of the program:

SET JAVA_HOME="C:\Program Files\Java7\Java.exe"

(This is just an example, the directory might be different on your computer)

If the program does not use such a batch (you can recognize it because it ends with either .cmd or .bat) create such a file and use that for launching the program instead:

@echo off
SET JAVA_HOME=...
ThisIsMyFancyScalaProgram.Exe
Lesialesion answered 15/6, 2015 at 21:59 Comment(0)
L
8

You may create 2 batch file one for java 7 and one for java 8 like this -

jdk7.bat

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.0_11\bin;%PATH%
echo Display java version
java -version

jdk8.bat

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.8_11\bin;%PATH%
echo Display java version
java -version

You may quickly switch between them running these batch file.

Loud answered 15/6, 2015 at 22:13 Comment(0)
L
4

If the program uses a batch to start, then add this line before the start of the program:

SET JAVA_HOME="C:\Program Files\Java7\Java.exe"

(This is just an example, the directory might be different on your computer)

If the program does not use such a batch (you can recognize it because it ends with either .cmd or .bat) create such a file and use that for launching the program instead:

@echo off
SET JAVA_HOME=...
ThisIsMyFancyScalaProgram.Exe
Lesialesion answered 15/6, 2015 at 21:59 Comment(0)
D
0

To Add system environment variables:

setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"

setx PATH "%PATH%;%JAVA_HOME%\bin";

To update system environment variables:

setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"

setx -m PATH "%PATH%;%JAVA_HOME%\bin";
Doggett answered 25/11, 2020 at 13:22 Comment(1)
Where is the improvement over Razibs answer ?Lesialesion

© 2022 - 2024 — McMap. All rights reserved.