在 Java 中运行 Python 脚本返回 python 退出代码 9009(无法午餐 python)

问题描述 投票:0回答:1

问题&&我在做什么 我编写了一个 Python 脚本,该脚本检查计算机的端口,以使用其 Windows 硬件 ID 搜索特定的阅读器,并相应地返回 True 或 False。问题是当我使用 BufferedReader 和 InputStreamReader 在 Java 环境中运行脚本时,Python 存在,代码为 9009。

Python 脚本应该根据连接到计算机的 USB 读卡器的状态返回 true 或 false 值。如前所述,该脚本在单独运行时有效(在 IntelliJ、PyCharm 和 VS Code 上)。当它尝试在我的 java 程序上运行时,它返回以下内容:

Python 脚本退出,代码为:9009

我尝试过的事情:

  1. 检查Python解释器/版本/等... |该代码在我的 java IDE (IntelliJ) 中运行
  2. 在外部运行代码以检查错误 |代码在其他地方都可以正常运行,只是在 java 上不行。
  3. 使用不同版本的文件路径(绝对路径和内容根目录内的路径)|不起作用,下面添加的代码使用绝对路径。

Python 脚本

import usbmonitor
from usbmonitor.attributes import ID_MODEL, ID_MODEL_ID, ID_VENDOR_ID


lookFor= "USB\\VID_058F&PID_9540\\5&54725E2&0&2"
def is_connected(lookFor):
    # Create the USBMonitor instance
    monitor = usbmonitor.USBMonitor()

    # Get the current devices
    devices_dict = monitor.get_available_devices()

    # Check if the device is connected
    for device_id, device_info in devices_dict.items():
        if device_id.split('\\')[1] == lookFor.split('\\')[1]:
            return True

    return False


# Look for the specific device
if is_connected("USB\\VID_058F&PID_9540\\5&54725E2&0&2"):
    print("Found")
else:
    print("Not Found")


if __name__ == "__main__":
    print("hello world from python file at: src/java/findUSB.py")
    print(is_connected(lookFor))

# Path: src\java\findUSB.py

Java代码

          public boolean usbConnected() {

        try {
            String pythonScript = "python3";  // Specify the Python interpreter to use (use "python" for Python 2)
            String scriptPath = "C:\\CS IA\\src\\java\\findUSB.py";  // Path to the Python script
            ProcessBuilder processBuilder = new ProcessBuilder(pythonScript, scriptPath);
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;


            boolean isConnected = false;  // Initialize the boolean variable

            while ((line = reader.readLine()) != null) {
                // Process  output of  Python script and update the boolean variable
                isConnected = Boolean.parseBoolean(line.trim());
            }

            int exitCode = process.waitFor();
            System.out.println("Python script exited with code: " + exitCode);

            // Now you can use the 'isConnected' variable to check the result
            if (isConnected) {
                System.out.println("The device is connected.");
                return true;
            } 
            else {
                System.out.println("The device is not connected.");
                return false;
            }
        } 
        catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return false;
        }

可能有用的信息:

脚本的绝对路径:C:\CS IA\src\java indUSB.py 根路径:src/java/findUSB.py python版本:Python 3.11.4

快速说明:我知道有一种使用专用 java 终端包进行此检查的简单方法,但是我想提高我的 python 技能,因此选择使用 python

python java intellij-idea bufferedreader
1个回答
0
投票

错误代码 9009 通常与系统找不到命令或可执行文件的问题相关。

  • Python 不在系统路径中
  • 指定Python脚本的路径不正确
© www.soinside.com 2019 - 2024. All rights reserved.