Hi, i needed to run a jar file from python code,but before running that file i want to know whether java is installed on the system or not, using the python code itself.Please help thanks
+3
A:
You could simply use the system commands.
e.g.
>>>import os
>>>os.system("java -version")
java version "1.5.0_19"
You will get the output, ofcourse assuming Java is in the classpath.
lud0h
2009-08-26 06:28:36
+1. That's imperfect, but really the closest to the right way to do it, provided Java has been installed properly, it should be in the PATH. I can't think of something better, unless using a full script to check all the possibilities accrod all platforms.
e-satis
2009-08-26 10:18:30
I think simply assigning to a variable and checking for zero (found Java) and non-zero (no Java in classpath) will be straight forward.
lud0h
2009-08-26 13:30:11
+2
A:
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
2009-08-26 06:35:48