java进程生成器添加环境不工作的路径

时间:2017-01-10 13:27:07

标签: java macos intellij-idea processbuilder

我在imac和mac书上使用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");

我得到例外:

java.io.IOException: Cannot run program "fslmaths" (in directory "~/IdeaProjects/test"): error=2, No such file or directory

为什么流程构建器不能在imac上找到/ usr / local / fsl / bin中的程序fslmath?

which fslmaths
/usr/local/fsl/bin/fslmaths
提前谢谢, 马丁

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。在“环境变量”下的intellij的“运行/调试配置”中,未启用“包含父环境变量”复选框。