-1

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%"
)

  • 2
    Never use `setx.exe` with the `PATH` variable! It will add all User Path values to the System path values, and as a result corrupt the resulting variable. Also using that format you'll also need to enable and use delayed expansion. If you want to check if `JAVA_HOME` exists, you should usually use `If` with `Defined` or `Not Defined`. – Compo Apr 04 '22 at 12:00
  • 1
    In addition, it is perfectly feasible that an existing PATH value may match the same location you want attributed to JAVA_HOME, regardless of whether that variable is defined. Your code does not take account of that, and would therefore potentially duplicate that too. – Compo Apr 04 '22 at 13:27
  • 1
    Please use the solution as provided by Gerhard. Otherwise if you really want to change persistent stored __system__ environment variables which requires elevated privileges of a local administrator, read really carefully [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) and [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564) for a *better* code to modify __user__ or __system__ `PATH`. Please note that I wrote *better*, but __not__ perfect. – Mofi Apr 04 '22 at 17:16

2 Answers2

1

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.)

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 1
    That is a very good answer with the best solution for the task as described in the question! – Mofi Apr 04 '22 at 16:58
  • 1
    Thanks @Mofi, I was not planning on answering but I felt I needed to set some things right else people will continue using methods that will cause more harm than good. – Gerhard Apr 04 '22 at 17:25
0

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

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 2
    That is not a good answer because of the usage of `SETX` with `%PATH%` or `!PATH!` is an absolute __NO GO - NEVER EVER__. It __corrupts__ irreversible without __SETX__ option `/M` the __user__ `PATH` and with option `/M` the __system__ `PATH` variable and so all __local__ `PATH`s derived later from __system__ and __user__ `PATH`. – Mofi Apr 04 '22 at 16:55