gpt4 book ai didi

java - 如何在 Mac 上从 Java 运行 Python3 文件

转载 作者:行者123 更新时间:2023-11-30 06:40:03 31 4
gpt4 key购买 nike

我使用标准 Java 例程来执行终端命令,但是我无法让它运行包含参数的 python 文件。

终端命令(在终端上运行)是:

python3 umlsConverter.py colon cancer

其中结肠癌是 N 个可能的字符串参数之一

我通常(从 Eclipse)运行来执行终端命令的 Java 例程是:

public static String execCmdV2(String cmd,String workingDirectoryPath) {

Runtime rt = Runtime.getRuntime();
//String[] commands = {"system.exe","-get t"};
String[] env= {};
Process proc;
File runDir = new File(workingDirectoryPath);
try {
proc = rt.exec(cmd,env,runDir);
} catch (IOException e1) {
System.out.println("Error executing command:" + e1.getLocalizedMessage());
return null;
}

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String fullOutputstring = null;
String s = null;
try {
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
fullOutputstring = fullOutputstring + s;
}
} catch (IOException e) {
System.out.println("Unable to output the results due to error:" + e.getLocalizedMessage());
return null;
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
try {
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
System.out.println("Unable to output the errors due to error:" + e.getLocalizedMessage());
return null;
}
return fullOutputstring;
}

当我运行例程时遇到的错误:

cmd = "python3 umlsConverter.py Breast cancer"

`workingDirectoryPath="/Users/n9569065/QuickUMLS"`

执行命令时出错:无法运行程序“python3”(在目录“/Users/n9569065/QuickUMLS”中):错误=2,没有这样的文件或目录

我认为问题与访问 python3 有关?

最佳答案

使用 python 可执行文件的完整路径。例如:/usr/bin/python3

关于java - 如何在 Mac 上从 Java 运行 Python3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44516245/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com