2017-07-22 6 views
0

現在、私はProcessを使用してJavaでpythonプログラムを起動できます。Javaからpythonスクリプトを実行しているときにmvnコマンドが見つかりません

問題は、Processが正常にインストールされていて、端末からPythonプログラムを実行できたにもかかわらず、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); 

私はProcessは、Pythonプログラム内mvnを認識するようにするJavaで何かできることはありますか?

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

Thxを!

編集

:私はこのポストを試してみました だから私は、私が実行している場合は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.

を意味

 //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によってコマンドが終了ので、コードを変更するHow do I launch a java process that has the standard bash shell environment?

/bin/bash -l -c "mvn --version"、Javaで127を終了しました。

+0

: あなたはPythonのサブプロセスを試すことができます。 – OldProgrammer

+0

@OldProgrammer Thxを思い出させるために、関連するコードを追加しました。 –

答えて

-1

Pytho nは端末からすべての環境を継承します。 Pythonから生成されたサブプロセスは、すべての環境を親プロセスから継承する必要があります。だから私はなぜmvnエラーが発生するか分からない。いくつかの関連するコードを表示してください

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

または

import subprocess 
args = ['mvn', '-version'] 
process = subprocess.Popen(args,shell=True) 
関連する問題