I found, we can find out Java version using System.getProperty("java.version")
.
from here - Getting Java version at runtime
But I have already done so coding using Runtime.getRuntime().exec(commands)
-
String[] commands ={"java", "-version"};
String line;
String cmdOutput = "";
try {
Process process = Runtime.getRuntime().exec(commands);
process.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = input.readLine()) != null) {
cmdOutput += (line + "\n");
}
System.out.println("output "+cmdOutput);
input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
But getting blank output.
When I run java -version
from command prompt, we get version, I feel it should also return same output.
Before I discard it and use System.getProperty("java.version")
, Can I please know what I am missing here ?
Thanks in advance.