java 创建进程问题 一个简单例子
我第一次用java的多进程,在网上看了个例子,照着执行了遍,根本就没有调用成功啊!很简单的一个例子,我就想在Test1中调用执行Test2,在f盘生成一个文件,可是一直没有成功。请大家指教一下是哪里的问题,路径吗?谢谢
Test1.java
package com.liuqiang;import java.io.IOException;public class Test1 { /** * @param args * @throws IOException */ public static void main(String[] args) { try { ProcessBuilder pb = new ProcessBuilder("java", "F:\\homework\\eclipse2\\ProcessTest\\bin\\com\\liuqiang\\Test2"); Process p = pb.start(); int a=p.exitValue(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
package com.liuqiang;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test2{ /** * @param args */ public static void main(String[] args) { FileOutputStream fOut=null; try { fOut = new FileOutputStream("E:\\Test2.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("被调用成功!"); }}
package IO;import java.io.File;import java.io.IOException;public class Test1 { /** * @param args * @throws IOException */ public static void main(String[] args) { try { ProcessBuilder pb = new ProcessBuilder("java","IO.Test2"); pb.directory(new File("D:\\WorkSpace\\Demo\\bin")); Process p = pb.start(); System.out.println(""); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}