[英]Java Runtime.exec() doesn't honor Linux alias
I have a compiled third party java program which uses Runtime.exec() to spawn a process but i wanted to add additional arguments to the process when the process starts using alias, but Java Runtime.exec() doesn't seem to honor. 我有一个已编译的第三方Java程序,该程序使用Runtime.exec()生成进程,但是我想在进程开始使用别名时向该进程添加其他参数,但是Java Runtime.exec()似乎不受欢迎。 I tried with my own program but still it doesn't see to work , any help ? 我尝试使用自己的程序,但仍然无法正常工作,有帮助吗?
import java.io.*;
public class Exec {
public static void main(String args[]) {
try {
String line;
Process p = Runtime.getRuntime().exec(args[0]);
BufferedReader bri = new BufferedReader
(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader
(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
p.waitFor();
System.out.println("Done.");
}
catch (Exception err) {
err.printStackTrace();
}
}
}
Output: 输出:
alias ls='ls -ltr'
java Exec ls
Exec.class
Exec.java
ls
-rw-r--r-- 1 user staff 1216 May 16 09:40 Exec.class
-rw-r--r-- 1 user staff 710 May 16 09:41 Exec.java
The reason is that alias is belonged to interactive shell process, so that the java can't see it. 原因是别名属于交互式Shell进程,因此Java无法看到它。
You can see the detail here https://unix.stackexchange.com/questions/1496/why-doesnt-my-bash-script-recognize-aliases 您可以在这里查看详细信息https://unix.stackexchange.com/questions/1496/why-doesnt-my-bash-script-recognize-aliases
If you want to execute the alias: 如果要执行别名:
Your shell is bash java Exec "bash -i -c 'ls'"
您的shell是bash java Exec "bash -i -c 'ls'"
Your shell is zsh java Exec "zsh -i -c 'ls'"
您的外壳是zsh java Exec "zsh -i -c 'ls'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.