intelliJ用のプラグインを開発中です。javaプロセスでmvnを実行できません
Javaコードの中にmvn
コマンドを呼び出す必要があります。しかし、不思議なこと、それは私に返しIOexception
:
Cannot run program "mvn" (in directory "/Users/ryouyasachi/Getty/semantiful-differentials-getty/getty/dsproj"):
error=2, No such file or directory
は、これは私のコードです:
/** @return: null if the process does not exit with 0
* @return: output if the process exits with 0
*/
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 null;
}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;
}
/**
*
* @param process which exits with 0
* @return The output string of the process
*/
private static String readOutput(Process process){
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder stringBuilder = new StringBuilder();
try {
while ((line = in.readLine()) != null) {
stringBuilder.append(line);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
PS:私はプロジェクトディレクトリの下にプロンプトに必要なMVNのコマンドを実行することができる午前 1これはプロジェクトディレクトリが存在し、mavenが適切にインストールされていることを示すはずです。
2.私のコードはgit
コマンドで正常に動作します。
'No such file or directory'はかなり明示的です。 'mvn'実行ファイルが見つかりません。パスは正しく設定されていますか? –
そうだと思います。私は砂場のインテリジェントIDEAでmvnを実行することができます。 'JAVA_HOME JAVA_HOME = /ライブラリ/ Javaの/ JavaVirtualMachines/jdk1.8.0_131.jdk /コンテンツ/ホーム 輸出JAVA_HOME M2_HOME = /ユーザ/ ryouyasachi/apacheの-のmaven-3.5.0 輸出M2_HOME PATH = $ PATH:$ JAVA_HOME/bin:$ M2_HOME/bin my.bash_profileにPATHをエクスポートする –