This question already has an answer here:
- Execute with parameters 2 answers
I am trying to run "java -version" using ProcessBuilder:
processBuilder = new ProcessBuilder("java -version");
process = processBuilder.start();
However I get an error:
java.io.IOException: Cannot run program "java -version": CreateProcess error=2, The system cannot find the file specified
When I remove the "-version" and do:
processBuilder = new ProcessBuilder("java");
process = processBuilder.start();
it runs fine and I get the normal help guide output.
How can I get it to run the argument too?
So you need to use:
You are probably making this unnecessarily complicated. If all you want to do is find out the version of Java you are running on, use
System.getProperty("java.specification.version")
.Also, your code will fail if Java is not on the PATH, but this way will still work.
The complete argument is being interpreted as the executable. Use