Using Python, I want to know whether Java is installed.
Asked
Active
Viewed 6,171 times
2 Answers
6
There is no 100% reliable / portable way to do this, but the following procedure should give you some confidence that Java has been installed and configured properly (on a Linux):
- Check that the "JAVA_HOME" environment variable has been set and that it points to a directory containing a "bin" directory and that the "bin" directory contains an executable "java" command.
- Check that the "java" command found via a search of "PATH" is the one that was found in step 1.
- Run the "java" command with "-version" to see if the output looks like a normal Java version stamp.
This doesn't guarantee that the user has not done something weird.
Actually, if it was me, I wouldn't bother with this. I'd just try to launch the Java app from Python assuming that the "java" on the user's path was the right one. If there were errors, I'd report them.
Stephen C
- 669,072
- 92
- 771
- 1,162
4
You could use the console commands
>>>import os
>>>os.system("java -version")
java version "1.5.0_19"
or see vartec's answer at Running shell command and capturing the output about storing the output