1
您好我想通過我的Java應用程序來運行一些Linux命令。當我運行下面的命令時,它既不會引發異常也不會提供輸出。但是,當我從linux命令提示符運行時,它執行得當。當我通過java應用程序給出「ls -al」命令時,它正常工作。那麼如何讓它工作?
以下是命令。
String cmd = "dir | grep gpc | grep -v 25";
以下是我的java程序
public class Test {
public static void main(String[] args) {
//String cmd = "ls -al";
String cmd ="dir | grep gpc | grep -v 25" ;
String property = System.getProperty("user.dir");
System.out.println("current directory:::: "+property);
String cmd =property+File.separator+"test1.sh" ;*/
//String cmd ="ls -al" ;
//String cmd = args[0];
String cmd = dir | grep gpc | grep -v 25;
System.out.println("executing command is:: "+cmd);
//String cmd ="dir | grep gpc | grep -v 25" ;
Runtime run = Runtime.getRuntime();
Process pr = null;
try {
pr = run.exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
try {
pr.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
BufferedReader buf = new BufferedReader(new InputStreamReader(
pr.getInputStream()));
String line = "";
try {
while ((line = buf.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}