Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether java is installed on a system through python?

Tags:

java

python

Using Python, I want to know whether Java is installed.

like image 720
kush87 Avatar asked Aug 26 '09 05:08

kush87


People also ask

How do you check if your system has Java installed?

Open command prompt and enter “java –version”. If installed version number is displayed. 2. On Windows, Java is usually installed in the directory C:/Program Files/Java.

How do I find where Java is installed?

On Windows 10 you can find out the path by going to Control Panel > Programs > Java. In the panel that shows up, you can find the path as demonstrated in the screenshot below.

How do I verify Java?

Step 1: Open Control Panel and click on Java icon. Step 2: In the Java Control Panel dialog box, click on About button. Step 3: About Java window appears, which shows the Java version.


2 Answers

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):

  1. 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.
  2. Check that the "java" command found via a search of "PATH" is the one that was found in step 1.
  3. 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.

like image 166
Stephen C Avatar answered Sep 30 '22 08:09

Stephen C


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

like image 30
lud0h Avatar answered Sep 30 '22 07:09

lud0h