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

*爱你&永不变心* 提交于 2020-01-11 07:20:09

问题


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!


回答1:


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"



回答2:


Go and get the latest Beanshell .jar file and put it into your Javasoft/jre/lib/ext directory.

Then , you can run Java shell scripts like this:

java bsh.Interpreter myscript.bsh [args]

Or, from within a Java program like so:

Object result = new bsh.Interpreter().source("myscript.bsh");

NOTE: in the second example, the args need to be set separately in the namespace.




回答3:


You can try,

bash -i -c "your command"

Here i stands for interactive screen, which will start by executing the commands present in the ~/.bashrc file



来源:https://stackoverflow.com/questions/7354592/how-do-i-launch-a-java-process-that-has-the-standard-bash-shell-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!