2

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

Good solution for Unix-based shells. But I don't think this will work on a Windows .bat file...
You are right, however the approach with java -version should work. So maybe where java or echo %JAVA_HOME% would help?
Yep! Please see my answer ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.