我使用标准的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”中):error = 2,没有这样的文件或目录
我认为问题与访问python3有关吗?
答案 0 :(得分:0)
使用python可执行文件的完整路径。例如:/ usr / bin / python3