Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 206
Joris Avatar asked Jun 23 '26 09:06

Joris


1 Answers

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")
like image 137
bmargulies Avatar answered Jun 26 '26 02:06

bmargulies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!