Java ProcessBuilder:找不到命令

发布于 2024-09-28 03:21:56 字数 874 浏览 0 评论 0原文

我正在尝试运行一些在 Linux 上找到的 Java 代码。

    maudecmd = new String[files.length+5];
    maudecmd[0] = "maude";
    maudecmd[1] = "-no-banner";
    maudecmd[2] = "-no-ansi-color";
    maudecmd[3] = "-no-mixfix";
    maudecmd[4] = "-no-wrap";
    for(int i = 0; i < files.length; ++i) {
        maudecmd[5+i] = files[i];
    }

    ProcessBuilder pb = new ProcessBuilder(maudecmd);
    if(dir != null)
        pb.directory(dir);
    pb.redirectErrorStream(true);
    maude = pb.start();

这会抛出 IOException - bash 找不到“maude”命令。

不过,我在 .bashrc 文件中有这个别名:

alias maude='~/lib/maude/maude.linux'

如果我像这样更改代码:

maudecmd[0] = "/u/h/os215/lib/maude/maude.linux";

它工作正常。

我想更改此代码,使其更加健壮 - 如果有人可以在命令行上运行 Maude,则 ProcessBuilder 也应该能够使用它,无论用户必须链接 Maude 的具体方法是什么。

这能实现吗?

I am trying to run some Java code I found on linux.

    maudecmd = new String[files.length+5];
    maudecmd[0] = "maude";
    maudecmd[1] = "-no-banner";
    maudecmd[2] = "-no-ansi-color";
    maudecmd[3] = "-no-mixfix";
    maudecmd[4] = "-no-wrap";
    for(int i = 0; i < files.length; ++i) {
        maudecmd[5+i] = files[i];
    }

    ProcessBuilder pb = new ProcessBuilder(maudecmd);
    if(dir != null)
        pb.directory(dir);
    pb.redirectErrorStream(true);
    maude = pb.start();

This throws an IOException - bash can't find the 'maude' command.

I have this aliased in my .bashrc file though:

alias maude='~/lib/maude/maude.linux'

If I change the code like this:

maudecmd[0] = "/u/h/os215/lib/maude/maude.linux";

It works fine.

I want to change this code so it is more robust - if someone can run Maude on the command line the ProcessBuilder should also be able to use it, whatever specific method the user has to link Maude up.

Can this be acheived?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

驱逐舰岛风号 2024-10-05 03:21:56

.bashrc 别名仅影响 BASH shell。您可以改为导出一个环境变量,但我不知道 Java 的 ProcessBuilder 是否会选择它。

话虽如此,我不明白为什么这行不通:
maudecmd[0] = "/bin/bash maude";

.bashrc aliases only affects the BASH shell. You could export an environment variable instead, but I don't know if Java's ProcessBuilder would pick that up either.

Having said that, I can't see why this wouldn't work:
maudecmd[0] = "/bin/bash maude";

花桑 2024-10-05 03:21:56

ProcessBuilder只能启动真正的进程。这里的 maude 是 bash 别名,因此仅适用于 bash。

您可以使用环境变量来指向可执行文件夹,而不是定义别名。或者只是将可执行路径作为应用程序的参数传递。

ProcessBuilder can only start real processes. Here maude is a bash alias, so only available for bash.

You can instead of define an alias use an environment variable to point to the executable folder. Or simply pass the executable path as an argument of your application.

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