[英]mvn command not found when running python script from Java
目前,我可以使用Process
在Java中啟動python程序。
問題是,盡管我已經正確安裝了maven並且能夠從終端運行python程序,但是Process
無法識別python程序中的mvn
命令。
這就是我使用Process
:
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;
}`
驅動程序代碼:
List<String> csi = new ArrayList<>();
CSI_PATH = getClass().getResource("/python/csi").getPath();
System.out.println("CSI path:" + CSI_PATH);
//Construct the argument
csi.add(CSI_PATH);
//argument for the csi program
csi.add(pre_hash);
csi.add(post_hash);
String csi_output = Command.runCommand(project_directory, csi);
System.out.println(csi_output);
我可以用Java做些什么讓Process
識別python程序中的mvn
嗎?
csi程序的相關部分:
os.sys_call("git checkout " + commit_hash)
os.sys_call("mvn clean")
bin_path = mvn.path_from_mvn_call("outputDirectory")
src_rel_path = mvn.path_from_mvn_call("sourceDirectory")
def path_from_mvn_call(env):
if env not in ["sourceDirectory", "scriptSourceDirectory", "testSourceDirectory",
"outputDirectory", "testOutputDirectory", "directory"]:
raise ValueError("incorrect env var: " + env)
mvn_cmd = "mvn help:evaluate -Dexpression=project.build." + env + " | grep ^/"
return subprocess.check_output(mvn_cmd, shell=True).strip()
def sys_call(cmd, ignore_bad_exit=False):
ret = subprocess.call(cmd, shell=True)
if ret != 0:
print "\n-- << non-zero exit status code >> --"
if ignore_bad_exit:
print "Exit from command: \n\t" + cmd
print "But we can safely ignore such non-zero exit status code this time.\n"
else:
print "Error in command: \n\t" + cmd + "\n"
raise SystemExit("system exit: " + str(ret))
提前謝謝!
編輯:我已經嘗試過這篇文章如何啟動具有標准bash shell環境的Java進程?
所以我將代碼更改為
//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,這意味着Value 127 is returned by your shell /bin/bash when any given command within your bash script or on bash command line is not found in any of the paths defined by PATH system environment variable.
如果我在Java中運行/bin/bash -l -c "mvn --version"
,它仍然會退出127。
Python從您的終端繼承了所有環境。 從Python產生的子流程應該繼承父流程的所有環境。 所以我不確定為什么會發生錯誤mvn。 您可以嘗試python subprocess:
import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args, stdout=subprocess.PIPE)
要么
import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args,shell=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.