gpt4 book ai didi

java - linux 命令未在 Java 应用程序中运行

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:00 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
How to use Pipe Symbol in Java

您好,我想通过我的 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();
}

}

}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com