如何在mac上从Java运行Python3文件
我使用一个标准Java例程来执行terminal命令,但是我无法让它运行一个包含参数的python文件 终端命令(在终端上工作)是: 其中结肠癌是N个可能的字符串参数之一 我通常运行(从Eclipse)来执行终端命令的Java例程是:如何在mac上从Java运行Python3文件,java,python,eclipse,Java,Python,Eclipse,我使用一个标准Java例程来执行terminal命令,但是我无法让它运行一个包含参数的python文件 终端命令(在终端上工作)是: 其中结肠癌是N个可能的字符串参数之一 我通常运行(从Eclipse)来执行终端命令的Java例程是: public static String execCmdV2(String cmd,String workingDirectoryPath) { Runtime rt = Runtime.getRuntime(); //String[] comm
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"
及
是
执行命令时出错:无法运行程序“python3”(在目录“/Users/n9569065/QuickUMLS”中):错误=2,没有这样的文件或目录
我认为问题与访问python 3有关。使用python可执行文件的完整路径。例如:/usr/bin/python3您告诉他在哪里使用python可执行文件?这是上面列出的WorkingDirectoryPath变量-它只是一个普通的用户目录。我的意思是:您不是告诉他使用pthon,而是告诉他在当前目录中启动一个名为“python3”的程序。这不是python的位置。好吧-那么如何更改运行python 3文件所需的工作?否则,如果您真的想启动python可执行文件,您可以执行
/bin/sh-c“python3 umlsConverter.py Breast cancer”
,这可以更方便地使用路径。我已经尝试过这个-我设置cmd=“/usr/bin/python3 umlsConverter.py乳腺癌”但结果类似:执行命令时出错:无法运行程序“/usr/bin/python3”(在目录“/Users/n9569065/QuickUMLS”中):error=2,没有此类文件或directory@WaterBoy标准macOS安装没有python3命令。默认情况下,唯一安装的python是Python2.7。我想我是用Brew安装的,所以它可能在其他地方吗?@WaterBoy我不使用Brew,但我相信它安装在/usr/local/bin或/usr/local/sbin中。
cmd = "python3 umlsConverter.py Breast cancer"
`workingDirectoryPath="/Users/n9569065/QuickUMLS"`