Why can't Python execute java.exe through a subprocess?

After upgrading Java from 1.6 to 1.7 x64 (on Windows 7), I cannot run java.exe through the Python 2.7 subprocess module. The following scripts are used only for work:

 import subprocess subprocess.check_call([r"C:\Windows\system32\java.exe"]) 

Now this is not happening:

 Traceback (most recent call last): File ".\tst.py", line 2, in <module> subprocess.check_call([r"C:\Windows\system32\java.exe"]) File "C:\Python27\lib\subprocess.py", line 506, in check_call retcode = call(*popenargs, **kwargs) File "C:\Python27\lib\subprocess.py", line 493, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 896, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified 

I also confirmed that C:\Windows\system32\java.exe really exists, is an application, and can be executed from the shell.

What is wrong here?

EDIT: I found that I can run C:\Program Files\Java\jre7\bin\java.exe from Python, so C:\Windows\system32\java.exe should be some kind of weird pseudo-shortcut, although technically this is a windows application. Version 1.7 should somehow mess it up, as I just confirmed that version 1.6 is fine.

+6
source share
2 answers

Suppose that the java.exe file in the "C: \ Windows \ System32" directory is not particularly safe. Even the assumption that the system "C: \ Windows \ System32" in the system is unsafe: Windows can be located on any fixed drive on the computer.

But even if there is "C: \ Windows \ System32 \ java.exe", it may not be visible for 32-bit processes under Win64. Windows does some interesting things here for backward compatibility, you can see http://en.wikipedia.org/wiki/WoW64 .

Finding the version of Java you are looking for - and there can be many - can be an ungrateful task. If you don’t really care about which Java you find, try the JAVA_HOME environment variable. It is not always there, but if so, you are done, and it is probably the most portable way to find the JVM. If this is not the case, you cannot go wrong by setting it, and many Java applications can use this variable.

Then again Java can be on PATH, and in this case, deleting everything except "java" in the subprocess call will do the trick. It does not hurt to try.

+8
source

You can also check if the PATH environment variable has quotation marks "" around the bin bin path. Python doesn't seem to like it:

  C:\bin>set PATH=C:\Python27;c:\Program Files\Java\jdk1.6.0_35\bin C:\bin>python -c "import subprocess; subprocess.Popen(['java', '-version'], stderr=subprocess.PIPE)" C:\bin>set PATH=C:\Python27;"c:\Program Files\Java\jdk1.6.0_35\bin" C:\bin>python -c "import subprocess; subprocess.Popen(['java', '-version'], stderr=subprocess.PIPE)" Traceback (most recent call last): [...] WindowsError: [Error 2] The system cannot find the file specified C:\bin> 
0
source

Source: https://habr.com/ru/post/915491/


All Articles