简体   繁体   English

为什么Python不能通过子进程执行java.exe?

[英]Why can't Python execute java.exe via subprocess?

After upgrading Java from 1.6 to 1.7 x64 (on Windows 7), I suddenly can't launch java.exe via Python 2.7's subprocess module anymore. 将Java从1.6升级到1.7 x64(在Windows 7上)后,我突然无法通过Python 2.7的subprocess模块启动java.exe。 The following script used to just work: 以下脚本用于工作:

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

Now it fails like this: 现在它失败了:

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 have also confirmed that C:\\Windows\\system32\\java.exe does indeed exist, is an application, and can be executed from the command shell. 我还确认C:\\Windows\\system32\\java.exe确实存在,是一个应用程序,并且可以从命令shell执行。

What goes wrong here? 这里出了什么问题?

EDIT: I've found that I can start C:\\Program Files\\Java\\jre7\\bin\\java.exe from Python, so C:\\Windows\\system32\\java.exe must be some weird pseudo-shortcut although technically a Windows application. 编辑:我发现我可以从Python启动C:\\Program Files\\Java\\jre7\\bin\\java.exe ,所以C:\\Windows\\system32\\java.exe必须是一些奇怪的伪捷径,尽管技术上是Windows应用。 Version 1.7 must've messed it up somehow, since I just confirmed Version 1.6 is fine. 版本1.7必须以某种方式弄乱它,因为我刚刚确认版本1.6很好。

Assuming that there is a java.exe at "C:\\Windows\\System32" is not a particularly safe assumption. 假设在“C:\\ Windows \\ System32”中有一个java.exe并不是一个特别安全的假设。 Even the assumption there is a "C:\\Windows\\System32" on the system isn't safe: Windows could reside on any fixed drive on the computer. 即使假设系统上有“C:\\ Windows \\ System32”也不安全:Windows可以驻留在计算机上的任何固定驱动器上。

But even if there is a "C:\\Windows\\System32\\java.exe", this might not be visible for 32-bit processes under Win64. 但即使存在“C:\\ Windows \\ System32 \\ java.exe”,Win32下的32位进程也可能看不到。 Windows does some interesting things here in the name of backwards compatibility, you might want to look at http://en.wikipedia.org/wiki/WoW64 . Windows在向后兼容性方面做了一些有趣的事情,您可能需要查看http://en.wikipedia.org/wiki/WoW64

Finding the Java version you're looking for - and there can be many - can be a thankless task. 找到你正在寻找的Java版本 - 并且可能有很多 - 可能是一个吃力不讨好的任务。 If you don't particularly care about which Java you find, try the JAVA_HOME environment variable. 如果你不特别在意 Java你发现,尝试JAVA_HOME环境变量。 It's not always there, but if it is, you're done and it's probably the most portable way of finding a JVM. 它并不总是存在,但如果是,那么你已经完成了,它可能是找到JVM最便携的方式。 If it's not there, you can't go wrong by setting it, and many Java applications can make use of that variable. 如果它不存在,那么通过设置它就不会出错,并且许多Java应用程序可以使用该变量。

Then again, Java just might be on the PATH, in which case removing the everything but 'java' in the subprocess call will do the trick. 然后,Java 可能就在PATH上,在这种情况下, 删除子进程调用中 “java”之外的所有东西都可以解决问题。 It can't hurt to try. 试试也不会有害。

You might also want to check if the PATH environment variable has quotation marks "" around the jre's bin path. 您可能还想检查PATH环境变量是否在jre的bin路径周围带有引号“”。 Python doesn't seem to like them: Python似乎不喜欢它们:

    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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过 python 脚本和 chaquopy 在 android 中为 stanfordtagger 启动 java.exe - Can't start java.exe for stanfordtagger in android via python script and chaquopy ProcessBuilder 执行 java.exe - ProcessBuilder execute java.exe 为什么java.exe在RuntimeException上不返回错误代码? - Why doesn't java.exe return error code on RuntimeException? 无法更新Android Studio:java.exe是否已修改? - Can't update Android Studio: java.exe is modified? 通过Windows Task Scheduler运行Java Programm不会关闭java.exe - Running Java Programm via Windows Task Scheduler doesn't close java.exe 为什么我不能在Windows上直接用Java执行Cygwin .exe? - Why can't I execute a Cygwin .exe directly with Java on Windows? 无法升级eclipse或Android ADK,也找不到java.exe…嗯? - Can't upgrade eclipse or Android ADK and can't find java.exe…hunh? 多个版本的“JDK”,哪个“java.exe”文件要执行? - Multiple versions of “JDK”, which “java.exe” file to execute? java.exe在当前工作目录中似乎找不到.java文件 - java.exe can't seem to find a .java file in the current working directory 无法运行:系统找不到文件C:\ProgramData\Oracle\Java\javapath\java.exe - Can't run: The system cannot find the file C:\ProgramData\Oracle\Java\javapath\java.exe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM