Basti
Basti

Reputation: 1190

Batch - Figure out if java is installed or not

First to say, I am not experienced with batch-scripts at all.

As the title already tells, I am trying to figure out if java is installed on the system or not, from a bat-file. I already checked many ways to do so but most of them seem more complex than what it should take to get this done.

My idea so far was to save the result of java -version into a variable and then check if there actually was an result, or if the command java couldn't be found. Sadly I couldn't even figure out, how to get the output of java -version saved to a variable so i could compare it.

If someone can help me with my idea or has a different simple solution to my problem i would be glad if you could help me out. Thanks.

Upvotes: 2

Views: 3393

Answers (2)

Paulo Mattos
Paulo Mattos

Reputation: 19339

Try this instead:

where java >nul 2>nul
if %errorlevel%==1 (
    @echo Java not found in path.
    exit
)

The >nul and 2>nul are optional and used only to mute the where command output.

Upvotes: 3

Sebastian
Sebastian

Reputation: 23

Try using java -version command in your script and saving it to a variable like this: isJavaInstalled="$(java -version)". If Java is installed, the variable should contain something like this: java version "1.8.0_131" Java(TM) SE Runtime Environment (build 1.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode), else the variable is empty. You can also try to check the value of JAVA_HOME system variable which should contain a path to where Java is installed.

Upvotes: 1

Related Questions