如何启动具有标准 bash shell 环境的 java 进程?

发布于 2024-12-03 10:02:48 字数 389 浏览 0 评论 0原文

我尝试过研究进程构建器,但我不确定如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼睛不忧郁 2024-12-10 10:02:48

您需要使用 ProcessBuilder 设置 shell 调用。执行如下命令:

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

在构建ProcessBuilder时,注意将要执行的命令作为一个统一的字符串传递,例如:

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

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")
心欲静而疯不止 2024-12-10 10:02:48

获取最新的 Beanshell .jar 文件并将其放入您的 Javasoft/jre/lib 中/ext 目录。

然后,您可以像这样运行 Java shell 脚本:

java bsh.Interpreter myscript.bsh [args]

或者,从 Java 程序中像这样运行:

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

注意:在第二个示例中,需要在命名空间中单独设置参数。

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.

叶落知秋 2024-12-10 10:02:48

您可以尝试,

bash -i -c "your command"

这里 i 代表交互式屏幕,它将通过执行 ~/.bashrc 文件中的命令来启动

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文