为什么Python不能执行java。通过子进程执行?
在将Java从1.6升级到1.7x64(在Windows 7上)之后,我突然无法启动Java。通过Python 2.7的subprocess
模块执行。以下脚本仅用于工作:
import subprocess
subprocess.check_call([r"C:\Windows\system32\java.exe"])
现在它失败了:
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
我还确认了C:\Windows\system32\java.exe
确实存在,是一个应用程序,可以从命令shell执行
这里出了什么问题
编辑:
我发现我可以从Python启动C:\Program Files\Java\jre7\bin\java.exe
,所以C:\Windows\system32\java.exe
一定是某种奇怪的伪快捷方式,虽然从技术上讲是Windows应用程序。1.7版一定是搞砸了,因为我刚刚确认1.6版没问题
# 1 楼答案
您可能还想检查PATH环境变量在jre的bin路径周围是否有引号“”。Python似乎不喜欢它们:
# 2 楼答案
假设有一个java。“C:\Windows\System32”中的exe不是一个特别安全的假设。即使假设系统上有一个“C:\Windows\System32”也不安全:Windows可以驻留在计算机上的任何固定驱动器上
但即使有“C:\Windows\System32\java.exe”,在Win64下的32位进程中也可能看不到。Windows以向后兼容性的名义做了一些有趣的事情,你可能想看看http://en.wikipedia.org/wiki/WoW64
找到您正在寻找的Java版本(可能有很多)可能是一项吃力不讨好的任务。如果您不特别关心找到哪个Java,请尝试Java_HOME环境变量。它并不总是存在,但如果存在,那么就完成了,这可能是找到JVM的最可移植的方式。如果它不存在,设置它就不会出错,许多Java应用程序都可以使用该变量
同样地,Java just可能会出现在路径上,在这种情况下,删除子流程调用中除“Java”之外的所有内容就可以了。试试也无妨