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
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(); } }
你在Runtime类中尝试过exec命令吗?
Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug")
运行时 - Java文档
为了避免在标准输出和/或错误输出大量数据时阻止被调用进程,您必须使用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(); }
您还可以像这样观看输出:
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”。
import java.io.*; Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
如果您遇到任何进一步的问题,请考虑以下问题,但我猜测上述内容对您有用:
Runtime.exec()的问题
Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");