Search code examples
javabashshellprocessenvironment

How do I launch a java process that has the standard bash shell environment?


I've tried looking into process builder, but I'm not sure how to source the bash environment into the process.

For example I'm using the following to launch my process:

Process p = new ProcessBuilder(args).start();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

And I'd like my standard shell environment (from /etc/profile, .bashrc, etc.) sourced to the process.

Sorry if I'm not using the right terms - still learning java.

Thanks in advance for any help!


Solution

  • You need to set up a shell invocation with ProcessBuilder. Execute a command like:

    /bin/bash -l -c "The entire command line that you want to execute"
    

    When constructing theProcessBuilder pay attention to pass the command to execute as one unified string, e.g:

    new ProcessBuilder("bash", "-l", "-c", "ps ax")