I have a problem with adding a path to the environment of a process using processbuider. I have no clue why the process is ignoring the environment. Here is my example:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
public class main {
public static void main(String [ ] args) {
try {
String s = null;
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "fsl");
Map<String, String> env;
env = pb.environment();
env.put("FSLDIR", "/usr/local/fsl/bin/");
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
System.out.println("Process p:");
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
//////////*********\\\\\\\\\\\
ProcessBuilder pb2 = new ProcessBuilder("/usr/local/fsl/bin/fsl");
s = null;
Process p2 = pb2.start();
stdInput = new BufferedReader(new
InputStreamReader(p2.getInputStream()));
stdError = new BufferedReader(new
InputStreamReader(p2.getErrorStream()));
System.out.println("Process p2:");
// 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 (Exception e) {
e.printStackTrace();
}
}
}
Output:
Process p:
/bin/bash: fsl: command not found
Process p2:
DISPLAY is not set. Please set your DISPLAY environment variable!
And you see FSL wants some more variables to be set. That is why p2 is not an option.
As Jdamian said, Bash searches the directories in the
PATH
environment variable to find binaries - Bash does not look for yourFSLDIR
variable, nor will it treat the values of arbitrary variables as executables.The first thing you should always do when running into issues with a process library (e.g.
ProcessBuilder
in Java,subprocess
in Python), is try to replicate the issue directly in the shell. If you runfsl
or/bin/bash -c fsl
in your shell you'll likely see the same error (if you don't you're not running your Java binary the same way as your shell) which confirms the issue is not related to Java.Once you've confirmed that it's just a question of how to fix it. If you intend for
fsl
to be always available add its containing directory to your PATH in your~/.bashrc
file:If you just want it available in your Java binary, modify the
PATH
in-process:In practice however, your code will often be much more maintainable and easier to work with if you don't modify the
PATH
and instead simply always invoke external processes by their absolute path, like your second example:These two commands are (roughly) equivalent, but the latter is far more clear, and less likely to introduce confusing bugs.