gpt4 book ai didi

java - 从 Java 运行 python 脚本时找不到 mvn 命令

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:08 26 4
gpt4 key购买 nike

目前我可以使用 Process 在 java 中启动 python 程序。

问题是,Process无法识别mvn python 程序中的命令,尽管我已经正确安装了 maven 并且能够从终端运行 python 程序。

这就是我使用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认识mvn在Python程序中?

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))

提前致谢!

编辑:我已经尝试过这个帖子 How do I launch a java process that has the standard bash shell environment?

所以我将代码更改为

      //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.

如果我运行/bin/bash -l -c "mvn --version"在java中,仍然退出127。

最佳答案

Python 从您的终端继承所有环境。从 Python 生成的子进程应该继承父进程的所有环境。所以我不确定为什么 mvn 会发生错误。你可以尝试 python 子进程:

import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args, stdout=subprocess.PIPE)

import subprocess
args = ['mvn', '-version']
process = subprocess.Popen(args,shell=True)

关于java - 从 Java 运行 python 脚本时找不到 mvn 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45250281/

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