在 Java 中运行命令行

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

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

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

319072 次浏览
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

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

您在 Runtime 类中尝试过 exec 命令吗?

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

运行时-Java 文档

import java.io.*;


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

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

与 Runtime.exec ()有关的问题

您还可以像下面这样观察输出:

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

为了避免被调用的进程在标准输出上输出大量数据和/或错误时被阻塞,您必须使用 Craigo 提供的解决方案。还要注意,ProcessBuilder 比 Runtime.getRuntime ()好。执行()。这是由于以下几个原因: 它更好地标记了参数,并且还处理了错误标准输出(也可以检查 给你)。

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();
}

怎么了

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();
}


}