user8874956
user8874956

Reputation: 159

How to validate that "java -version" doesn't returns an error through the code?

In our organization, we supply an installer (Install4j) to build and run our product.

On one of the phases there we make a validation for the java version with System.getProperty("java.version"). In some rare cases, when typing java -version from the command prompt, it can gets a variety of errors, like:

enter image description here

The problem I facing is that these kind of errors are getting displayed only from the command prompt, while System.getProperty("java.version") won't alert any, just return the version number.

I'm looking for a way monitoring these kind of errors and stop the installer accordingly, but all my attempts were in vain.

EDIT:

I thought of getting use Runtime.getRuntime().exec(), but nonetheless the 'java -version' response won't come back.

Upvotes: 2

Views: 230

Answers (2)

Ingo Kegel
Ingo Kegel

Reputation: 48090

With a "Run executable or batch file" action, set the "Executable" property to

${installer:sys.javaHome}/bin/java.exe

the "Arguments" property to

-version

and the "Redirect stderr" property to "To installer variable" with the child property "Installer variable name" set to

javaVersionOutput

After the action has run, you can use a "Run script" property to inspect the output:

String output = context.getVariable("javaVersionOutput");
// TODO check output

Upvotes: 2

Ali Azam
Ali Azam

Reputation: 2115

Use below code to handle the error and get the alert message:

String javaVersion = System.getProperty("java.version");

if(!javaVersion.contains("1.7")) {
    System.out.println("jdk 1.7 is required.");
    System.out.println("Please install jdk 1.7");
}

Upvotes: 0

Related Questions