【问题标题】:Running Command Line in Java [duplicate]在Java中运行命令行[重复]
【发布时间】:2012-01-19 18:08:47
【问题描述】:

有没有办法在 Java 应用程序中运行这个命令行?

java -jar map.jar time.rel test.txt debug

我可以用命令运行它,但我不能在 Java 中运行它。

【问题讨论】:

  • 我试过了,Runtime rt = Runtime.getRuntime();Process proc = rt.exec("ping localhost");
  • 你想从另一个虚拟机中启动一个虚拟机吗?

标签: java command-line runtime.exec


【解决方案1】:

你也可以这样看输出:

final Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");

new Thread(new Runnable() {
    public void run() {
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;

        try {
            while ((line = input.readLine()) != null)
                System.out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

p.waitFor();

别忘了,如果你正在运行一个 windows 命令,你需要在你的命令前面加上cmd /c

编辑:对于奖励积分,您还可以使用ProcessBuilder 将输入传递给程序:

String[] command = new String[] {
        "choice",
        "/C",
        "YN",
        "/M",
        "\"Press Y if you're cool\""
};
String inputLine = "Y";

ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

writer.write(inputLine);
writer.newLine();
writer.close();

String line;

while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

这将运行 windows 命令choice /C YN /M "Press Y if you're cool" 并以Y 响应。因此,输出将是:

Press Y if you're cool [Y,N]?Y

【讨论】:

  • 你也可以使用 p.getErrorStream 来了解你的命令为什么会出错!
  • @Craigo:关于您的“cmd /c”语句的任何参考资料?谢谢
  • 其实,我认为你只需要“cmd /c”,如果你想运行一个windows命令,比如“copy”。为混乱道歉。
  • 这不会为我打印整个输出。执行“ls”每次打印不同数量的目录内容。这对我最有效:alvinalexander.com/java/edu/pj/pj010016
【解决方案2】:

为了避免被调用的进程在标准输出和/或错误输出大量数据时被阻塞,您必须使用Craigo提供的解决方案。另请注意,ProcessBuilder 优于 Runtime.getRuntime().exec()。这有几个原因:它更好地标记了参数,并且还处理了错误标准输出(另请参阅here)。

ProcessBuilder builder = new ProcessBuilder("cmd", "arg1", ...);
builder.redirectErrorStream(true);
final Process process = builder.start();

// Watch the process
watch(process);

我使用一个新函数“watch”在一个新线程中收集这些数据。当被调用进程结束时,该线程将在调用进程中结束。

private static void watch(final Process process) {
    new Thread() {
        public void run() {
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null; 
            try {
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
}

【讨论】:

  • 感谢您指出这一点,我实际上很挣扎,因为我没有标准错误。
【解决方案3】:

怎么样

public class CmdExec {

public static Scanner s = null;


public static void main(String[] args) throws InterruptedException, IOException {
    s = new Scanner(System.in);
    System.out.print("$ ");
    String cmd = s.nextLine();
    final Process p = Runtime.getRuntime().exec(cmd);

    new Thread(new Runnable() {
        public void run() {
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null; 

            try {
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    p.waitFor();
     }

 }

【讨论】:

    【解决方案4】:
    Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
    

    【讨论】:

      【解决方案5】:
      import java.io.*;
      
      Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
      

      如果您遇到任何进一步的问题,请考虑以下内容,但我猜以上内容对您有用:

      Problems with Runtime.exec()

      【讨论】:

        【解决方案6】:

        您是否尝试过 Runtime 类中的 exec 命令?

        Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug")
        

        Runtime - Java Documentation

        【讨论】:

          【解决方案7】:
          Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
          

          【讨论】:

          • 最短的代码永远是我的最爱。
          【解决方案8】:
          Runtime rt = Runtime.getRuntime();
          Process pr = rt.exec("java -jar map.jar time.rel test.txt debug");
          

          http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

          【讨论】:

          • 当我尝试将它输出到控制台时它只输出如下内容是否正常:java.lang.ProcessImpl@6ecec5288
          • 使用pr.getInputStream()。这是一个详细的示例:linglom.com/2007/06/06/…
          • 检查进程返回的内容很有用。你可以用 pr.waitFor() 来实现。所以它看起来像这样:int retVal = pr.waitFor()。所以如果不为0,可以中止/清理。
          • pr.exitValuepr.waitFor() 之间是否存在实际/有意义的区别?更新:我认为如果进程没有完成,exitValue 会抛出异常,因为 waitFor... 你知道
          • 没有什么比“Runtime rt = Runtime.getRuntime();”更java了
          猜你喜欢
          • 1970-01-01
          • 2012-08-19
          • 1970-01-01
          • 1970-01-01
          • 2014-10-01
          • 1970-01-01
          • 2012-04-19
          • 2021-07-16
          • 1970-01-01
          相关资源
          最近更新 更多