从Java运行python脚本时未找到mvn命令
目前,我能够使用从Java运行python脚本时未找到mvn命令,java,linux,shell,maven,Java,Linux,Shell,Maven,目前,我能够使用Process在java中启动python程序 问题是,Process无法识别python程序中的mvn命令,尽管我已经正确安装了maven,并且能够从终端运行python程序 这就是我如何使用过程: public static String runCommand(String directory, List<String> command) { ProcessBuilder processBuilder = new ProcessBuilder(comma
Process
在java中启动python程序
问题是,Process
无法识别python程序中的mvn
命令,尽管我已经正确安装了maven,并且能够从终端运行python程序
这就是我如何使用过程:
public static String runCommand(String directory, List<String> command) {
ProcessBuilder processBuilder = new ProcessBuilder(command).directory(new File(directory));
processBuilder.redirectErrorStream(true);
Process process;
String output = null;
try {
process = processBuilder.start();
//Pause the current thread until the process is done
process.waitFor();
//When the process does not exit properly
if (process.exitValue() != 0) {
//Error
System.out.println("command exited in error: " + process.exitValue());
//Handle the error
return readOutput(process);
}else {
output = readOutput(process);
System.out.println(output);
}
} catch (InterruptedException e) {
System.out.println("Something wrong with command: " +e.getMessage());
} catch (IOException e) {
System.out.println("Something wrong with command: " +e.getMessage());
}
return output;
}`
提前谢谢
编辑:
我试过这个帖子
所以我把代码改为
//Construct the argument
csi.add("/bin/bash");
csi.add("-l");
csi.add("-c");
csi.add("\"" + csi_path + " " + pre_hash+ " " + post_hash + "\"");
String csi_output = Command.runCommand(project_directory, csi);
但即使如此,命令仍以127退出,这意味着当在PATH系统环境变量定义的任何路径中都找不到bash脚本中或bash命令行上的任何给定命令时,shell/bin/bash将返回值127。
如果我在java中运行/bin/bash-l-c“mvn--version”
,它仍然会退出127。Python从终端继承所有环境。从Python派生的子进程应该继承父进程的all环境。因此,我不确定为什么会出现错误mvn。
您可以尝试python子流程:
import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args, stdout=subprocess.PIPE)
或
请显示一些相关代码。@OldProgrammer Thx对于提醒,我已经添加了相关代码。
//Construct the argument
csi.add("/bin/bash");
csi.add("-l");
csi.add("-c");
csi.add("\"" + csi_path + " " + pre_hash+ " " + post_hash + "\"");
String csi_output = Command.runCommand(project_directory, csi);
import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args, stdout=subprocess.PIPE)
import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args,shell=True)