我在我的 imac 和 mac book 上都使用 intellij。当我在我的 mac 书上运行以下代码时,一切正常。
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Main {
public ProcessBuilder pb;
public Main(){
try {
pb = new ProcessBuilder();
pb.directory(new File("~/IdeaProjects/test"));
Map<String, String> env;
env = pb.environment();
env.put("PATH", "/usr/local/fsl/bin/");
} catch (Exception e) {
e.printStackTrace();
}
}
public void getMeanImage(String base, String file){
List<String> cmd = new LinkedList<>();
cmd.add("fslmaths");
cmd.add(base + file);
cmd.add("-Tmean");
cmd.add(base + file + "_mean");
pb.command(cmd);
try {
String s = "";
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [ ] args) {
Main m = new Main();
m.getMeanImage("", "scan.nii.gz");
}
}
在 imac 上我遇到了问题。我复制了 printenv 使用的 PATH 值。
env.put("PATH", "/usr/local/fsl/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin");
我得到了异常(exception):
java.io.IOException: Cannot run program "fslmaths" (in directory "~/IdeaProjects/test"): error=2, No such file or directory
为什么进程构建器在 imac 上的/usr/local/fsl/bin 中找不到程序 fslmaths?
which fslmaths
/usr/local/fsl/bin/fslmaths
提前致谢, 马丁
最佳答案
我找到了解决方案。在 intellij 的“运行/调试配置”中,“环境变量”下的“包含父环境变量”复选框未启用。
关于java进程构建器添加环境路径不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41569985/