Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discover from a batch file where is Java installed?

I want to set the JAVA_HOME variable from a batch script

like image 315
flybywire Avatar asked Mar 12 '09 11:03

flybywire


People also ask

How do you find out where my Java is installed?

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.

How do I know if Java is installed in a batch file?

Going to a command line and typing java -version can tell us for sure if Java is installed.

How do you check Java is installed or not?

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 .


2 Answers

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%
)
like image 76
Patrick Cuff Avatar answered Nov 15 '22 06:11

Patrick Cuff


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.

like image 25
Ian Kemp Avatar answered Nov 15 '22 04:11

Ian Kemp