有没有一种方法可以在Java应用程序中运行这个命令行?
java -jar map.jar time.rel test.txt debug
我可以使用命令运行它,但是我无法在Java中运行它。
有没有一种方法可以在Java应用程序中运行这个命令行?
java -jar map.jar time.rel test.txt debug
我可以使用命令运行它,但是我无法在Java中运行它。
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
pr.getInputStream()
方法。以下是详细示例:http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/ - kolpr.waitFor()
获得该值。代码如下:int retVal = pr.waitFor()
。如果返回值不为0,则可以中止/清理操作。 - Apachepr.exitValue
和pr.waitFor()
之间有实际/有意义的区别吗?更新:我认为如果进程尚未完成,exitValue将抛出异常,而waitFor...您知道。 - Don Cheadle你也可以像这样查看输出:
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 "如果你很酷,请按Y"
并响应Y
。因此,输出将是:
Press Y if you're cool [Y,N]?Y
为避免当被调用的进程在标准输出和/或错误流上输出大量数据时被阻塞,您需要使用Craigo提供的解决方案。还请注意,ProcessBuilder优于Runtime.getRuntime().exec()。原因有几个:它更好地对参数进行了分词,并且还负责处理错误的标准输出(也可以在这里查看)。
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();
}
Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
import java.io.*;
Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
那么呢?
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();
}
}
Process p = 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")