https://www.cnblogs.com/fnlingnzb-learner/p/11664000.html
1.java进程间通信
1. Runtime+.Process可以启动新的进程
Runtime rt = Runtime.getRuntime(); Process exec = rt.exec("shutdown -a"); exec.waitFor(); //当前进程阻塞,直到调用的进程运行结束。 System.out.println("exec.exitValue()="+exec.exitValue()); //正常结束时,子进程的返回值为0
2.ProcessBuilder+Process
ProcessBuilder pb = new ProcessBuilder("javac", "-d",".","src/testExcel/D.java"); try { Process p = pb.start(); p.waitFor(); System.out.println(p.exitValue()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
3.父进程获取子进程的返回值
exec2.waitFor();直到调用的进程运行结束,否则可能获取到的输出不完整。
Runtime rt = Runtime.getRuntime(); Process exec2 = rt.exec("java testExcel/D"); // 运行D.class对应的字节码文件 exec2.waitFor(); System.out.println("exec2.exitValue()="+exec2.exitValue()); OutputStream outputStream = exec2.getOutputStream(); InputStream inputStream = exec2.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); while(reader.ready()) System.out.println("reader.readLine()"+reader.readLine());
测试程序代码如下:
package testExcel; public class D{ public static void main(String[] args) { System.out.println("D out1"); System.out.println("D out2"); System.out.println("D out3"); } }
package child; import java.io.*; import java.nio.CharBuffer; import java.util.Arrays; public class ProcessCommunication{ public static void main(String[] args) { System.out.println(System.getProperty("user.dir")); //D:TMPEclipseInternalValClu File file = new File("../test.txt"); try { if(!file.exists()) file.createNewFile(); //会创建于一个文件,在D:TMPEclipse est.txt } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } System.out.println(file.getAbsolutePath()); //、 try { System.out.println(file.getCanonicalPath()); //D:TMPEclipse est.txt } catch (IOException e1) { e1.printStackTrace(); } System.out.println(file.getPath()); //.. est.txt System.out.println(); Runtime rt = Runtime.getRuntime();try { // 执行一些命令,启动进程 // Process exec = rt.exec("shutdown.exe -s -t 3000"); Process exec = rt.exec("shutdown -a"); // rt.exec("mstsc"); exec.waitFor(); //当前进程阻塞,直到调用的进程运行结束。 System.out.println("exec.exitValue()="+exec.exitValue()); //正常结束时,子进程的返回值为0 Process exec1 = rt.exec("javac -d . src/testExcel/D.java"); // 执行编译D.java文件的命令 exec1.waitFor(); System.out.println(exec1.exitValue()); Process exec2 = rt.exec("java testExcel/D"); // 运行D.class对应的字节码文件 exec2.waitFor(); System.out.println("exec2.exitValue()="+exec2.exitValue()); OutputStream outputStream = exec2.getOutputStream(); InputStream inputStream = exec2.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); while(reader.ready()) System.out.println("reader.readLine()"+reader.readLine()); byte[] b =new byte[100]; inputStream.read(b); System.out.println("new String(b)"+new String(b,"utf-8")); System.out.println("查看内存"); System.out.println(rt.freeMemory()+" "+rt.totalMemory()+" "+rt.maxMemory()/1024/8+" "+rt.availableProcessors()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } ProcessBuilder pb = new ProcessBuilder("javac", "-d",".","src/testExcel/D.java"); try { Process p = pb.start(); p.waitFor(); System.out.println(p.exitValue()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } ProcessBuilder pb2 = new ProcessBuilder("java", "testExcel/D"); try { Process p2 = pb2.start(); p2.waitFor(); System.out.println(p2.exitValue()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
D:TMPEclipseInternalValClu D:TMPEclipseInternalValClu.. est.txt D:TMPEclipse est.txt .. est.txt exec.exitValue()=1116 0 exec2.exitValue()=0 reader.readLine()D out1 reader.readLine()D out2 reader.readLine()D out3 new String(b)