如何启动具有标准 bash shell 环境的 java 进程?
我尝试过研究进程构建器,但我不确定如何将 bash 环境引入到进程中。
例如,我使用以下命令来启动我的进程:
Process p = new ProcessBuilder(args).start();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
我希望我的标准 shell 环境(来自 /etc/profile、.bashrc 等)源自该进程。
抱歉,如果我没有使用正确的术语 - 仍在学习 java。
预先感谢您的任何帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用 ProcessBuilder 设置 shell 调用。执行如下命令:
在构建
ProcessBuilder
时,注意将要执行的命令作为一个统一的字符串传递,例如:You need to set up a shell invocation with ProcessBuilder. Execute a command like:
When constructing the
ProcessBuilder
pay attention to pass the command to execute as one unified string, e.g:获取最新的 Beanshell .jar 文件并将其放入您的 Javasoft/jre/lib 中/ext 目录。
然后,您可以像这样运行 Java shell 脚本:
或者,从 Java 程序中像这样运行:
注意:在第二个示例中,需要在命名空间中单独设置参数。
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:
Or, from within a Java program like so:
NOTE: in the second example, the args need to be set separately in the namespace.
您可以尝试,
这里 i 代表交互式屏幕,它将通过执行
~/.bashrc 文件中的命令来启动
You can try,
Here i stands for interactive screen, which will start by executing the commands present in the
~/.bashrc file