Java ProcessBuilder:找不到命令
我正在尝试运行一些在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.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";
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.