1

I am using intellij both on my imac and mac book. when i run the following code on my mac book, everything works.

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");
    }
}

On the imac I run into problems. I copied the PATH value used by printenv.

env.put("PATH", "/usr/local/fsl/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin");

I get the exception:

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

Why can't the process builder find the program fslmaths in /usr/local/fsl/bin on the imac?

which fslmaths
/usr/local/fsl/bin/fslmaths

thanks in advance, Martin

1
  • Is pb.directory(new File("~/IdeaProjects/test")) actually creating a new subdirectory in your home directory, and not a directory literally named ~? Commented Jan 10, 2017 at 13:55

1 Answer 1

2

I found the solution. In the 'Run/Debug Configurations' of intellij under 'Environment Variables' the checkbox 'Include parent environment variables' was not enabled.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.