java调python脚本的调到了 但是没执行就返回了n_从Java运行python脚本时找不到mvn命令...

1586010002-jmsa.png

Currently I am able to launch a python program in java using Process.

The problem is that, Process cannot recognize mvn command in the python program, although I have maven installed properly and was able to run python program from terminal.

This is how I use Process:

public static String runCommand(String directory, List 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;

}`

Driver code:

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

Is there anything I can do in Java to let the Process to recognize mvn inside the python program?

relevant part of csi program:

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

Thx in advance!

So I changes the code to

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

But even so the command exits by 127, which means 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.

If I run /bin/bash -l -c "mvn --version" in java, it still exited 127.

解决方案

Python inherits the all environment from your terminal. a subprocess spawned from Python should inherit the all environment from the parent process. So I am not sure why the error occur mvn.

you can try python subprocess :

import subprocess

args = ['mvn', '-version']

process = subprocess.Popen(args, stdout=subprocess.PIPE)

or

import subprocess

args = ['mvn', '-version']

process = subprocess.Popen(args,shell=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值