How to set custom JAVA_HOME if JAVA_HOME does not exist yet.
if "%JAVA_HOME%" == "" (
echo Setting JAVA_HOME
set jdk=%~dp0openjdk-10.5.10
echo %jdk%
SETX /M JAVA_HOME "%jdk%"
SETX /m PATH "%path%;%jdk%"
)
CodePudding user response:
I would not use the code block at all, in fact, I would not do this at all, I am just posting a much simple solution to your problem:
if defined JAVA_HOME goto :EOF
set "jdk=%~dp0openjdk-10.5.10"
echo %jdk%
This will simply fall through the if defined
statement, should the variable not exist.
This is still a bad idea as this assumes that the java directories exist on the drive and in the path of the batch-file
. I am more concerned with the earlier question where you wanted to mach java versions, which tells me you are unsure of what is/is not installed thus far.
I will not use setx
here as it WILL destroy your path
variable. Please also make a copy of your path
variables before you attempt to use setx
on it (though still not recommended.)
CodePudding user response:
try like this:
setlocal enableDelayedExpansion
if "%JAVA_HOME%" == "" (
echo Setting JAVA_HOME
set "jdk=%~dp0openjdk-10.5.10"
echo !jdk!
SETX /M JAVA_HOME "!jdk!"
SETX PATH "!PATH!;!jdk!"
)
more about the delayed expansion -> https://ss64.com/nt/delayedexpansion.html