0

I have a test.bat file where i need to write a script to check PATH VARIABLE for java already installed or not ?

 Conditions :-

  1) If yes then 
       a) Check for the java version .
       b) Check for JAVA_HOME env variable :-
           i) if no , create it.
           ii) if yes , get the JAVA_HOME value.
  2) If No then
   set JAVA_HOME to local path (embedded)



 I know the commands like :-
      For version
         -> java -version

      For variable path 
         -> echo %path%

      For Java variable path
         -> echo %JAVA_HOME%

But how to write script for these condition in batch file i.e .bat ? I'm very new to this so kindly give me your valuable answers or helpful links.

3 Answers 3

3

You can check with something like that:

@echo off
if defined JAVA_HOME (
if exist "%JAVA_HOME%\bin\javac.exe" goto okJavac
)

echo Java not found
exit 1

%okJavac
echo Java path "%JAVA_HOME%"
rem the end of your script
Sign up to request clarification or add additional context in comments.

4 Comments

:I tried your code but after double clicking on the test.bat file but i could see a glimpse of terminal which comes and disappear .Even i checked JAVA_HOME path location and its in right location as what you defined in your code. How can i resolve this problem ?
@Tala : any solutions for this ?
Start your bat from cmd with K key added : /K yourfile.bat
i tried :okJavac instead of %okJavac ....and its working . thanx Tala and Alexxx :)
2

try this:

@ECHO OFF &SETLOCAL
for /f tokens^=2delims^=^"  %%a in ('java -version 2^>^&1') do set "JavaVersion=%%a"
IF NOT DEFINED JavaVersion ECHO no Java installed & GOTO :EOF
FOR /f "tokens=2delims=." %%a IN ("%JavaVersion%") DO SET "sub=%%a"

IF DEFINED ProgramFiles(x86) (
    IF NOT DEFINED JAVA_HOME SET "JAVA_HOME=%ProgramFiles(x86)%\Java\jre%sub%\bin"
) ELSE (
    IF NOT DEFINED JAVA_HOME SET "JAVA_HOME=%ProgramFiles%\Java\jre%sub%\bin"
)

SET "java_home"
PAUSE

Comments

1

Check the appropriate MS guide for working with the batch files. You can check the output of the java -version in the way described here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.