0

I'm a MR tool admin I need a batch script to satisfy the below requirements

  1. It must check whether Java home is set in Computer > properties (right click) > Advanced system settings > Advanced > Environment Variables.

    If Java home is available it needs to display "Java home is available" else "Java home is not available".

  2. It must check the java version using and if it is greater than 1.6.495 it must display "Java version is too high" else "Java version is compatible".

Currently I'm using a script as given below... Can anyone please please help for making the above script?

@echo off
IF "%JAVA_HOME%" == "" (
    echo Enter path to JAVA_HOME: 
    set /p JAVA_HOME=
) ELSE (
    echo %JAVA_HOME%
)
Community
  • 1
  • 1
Van
  • 635
  • 1
  • 6
  • 10

1 Answers1

1

Give this a whirl ...

I wasn't quite sure what you mean by "java home is available". The script checks that the directory pointed to by %JAVA_HOME% contains a bin\java.exe.

The tricky bit is getting the version. I took the output of java -version and used FIND to pick up the first line which is something like java version "1.7.0_55" on my system. java -version writes this to stderr rather than stdout, hence the error redirect (2>& 1 redirects error output to standard output) before piping to FIND. That mess is then redirected into a temp file. Then I used a SET /p with input redirected from the temp to put that output into a variable.

@echo off
SETLOCAL
SET TEMPFILE=%TEMP%\tmpfile
IF "%JAVA_HOME%" == "" (
    echo Enter path to JAVA_HOME:
    set /p JAVA_HOME=
) ELSE (
    echo JAVA_HOME = %JAVA_HOME%
)

IF EXIST "%JAVA_HOME%\bin\java.exe" (
    ECHO Java home is available
) ELSE ECHO Java home is not available

"%JAVA_HOME%\bin\java" -version 2>& 1 | FIND "java version" > %TEMPFILE%
SET /p VERSIONSTRING= < %TEMPFILE%
DEL %TEMPFILE%
SET MAJORVERSION=%VERSIONSTRING:~14,1%
SET MINORVERSION=%VERSIONSTRING:~16,1%
SET UPDATEVERSION=%VERSIONSTRING:~20,-1%
IF %MAJORVERSION% GTR 1 GOTO TOOHIGH
IF %MINORVERSION% GTR 6 GOTO TOOHIGH
IF %UPDATEVERSION% GTR 495 GOTO TOOHIGH
ECHO Java version is compatible.
GOTO EXIT

:TOOHIGH
ECHO Java version is too high.

:EXIT
ENDLOCAL
Beel
  • 1,000
  • 12
  • 23
  • Hai Beel: Thanks for you valuable help but when I'm running the script it is asking "Enter the path to Java home" so what should I do?which path that I need to give.I want to avoid this also it needs to check whetehr the java home is set in the system and needs to display the error.What are the modifications that needed for this script. – Van Jul 11 '14 at 07:08
  • Manu, that part comes from the script you provided at the start. It means that JAVA_HOME has not been set on that machine. Do you just want to display an error instead? As written, you should provide it the path to a directory containing a JRE (on my machine, c:\program files\java\jdk1.7.0_55). Sounds as if you are quite a bit out of your depth -- who are you writing this program for? – Beel Jul 11 '14 at 10:36
  • Hi beel I'm not familiar with batch scripts and windows environment.since Im working in unix environment.My actual requirement is if we are going to this path : Computer > properties (right click) > Advanced system settings > Advanced > Environment Variables "c:\program files\java\jre" must set as java home value. And the script needs to display java home is available if the value is defined in the mentioned path(in environment variable) .Is it possible? Thanks for your patience and replies – Van Jul 13 '14 at 05:47
  • Manu, I removed jre from my script (that was a mistake on my part). This script will work now for the case you describe. Does this do what you need? If not, please edit your question at the top to clarify. – Beel Jul 13 '14 at 11:42
  • Beel: Still is asking for enter the path to java home.Which path dat I need to mention here? – Van Jul 14 '14 at 07:39
  • Beel: Java home is set in Computer>Properties> but i think it is not possible to go to that from command prompt – Van Jul 14 '14 at 08:40
  • Manu, if you do not want the prompt, you can just delete the lines that you included in your original script (4-9). What do you want to happen if JAVA_HOME is not set at all? You want them to be able to set it? Will they know what to set it to better than you do? – Beel Jul 14 '14 at 12:29
  • Beel: If JAVA_Home is not set at all I need to display an error message that "Java_Home is not available" in a dialog box is it possible? – Van Jul 14 '14 at 15:27