I want to set the JAVA_HOME variable from a batch script
On Windows, Java is usually installed in the directory C:/Program Files/Java. You can check if this folder exists. If the folder does not exist, we can't be sure that Java is not installed on your computer. It could have been installed in a different path.
Going to a command line and typing java -version can tell us for sure if Java is installed.
1)Open the command prompt or terminal based on your OS. 2)Then type java --version in the terminal. 3) If java is installed successfullly it will show the respective version .
This snippet will search the current PATH for java.exe, and print out where it was found:
for /f %%j in ("java.exe") do @echo.%%~dp$PATH:j
On my system this gives me
C:\WINDOWS\system32\
Using this you can set JAVA_HOME as follows:
@echo off
for /f %%j in ("java.exe") do (
set JAVA_HOME=%%~dp$PATH:j
)
if %JAVA_HOME%.==. (
@echo java.exe not found
) else (
@echo JAVA_HOME = %JAVA_HOME%
)
If JAVA_HOME isn't already set, and you want to set it, then you probably need to do something like
dir java.exe /B /S
which will give you a list of all directories containing the file java.exe. From there you can pipe that output to another command that parses the results and selects one of those directories, then uses it to set the JAVA_HOME variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With