使用Runtime.getRuntime().exec()方法可以在java程序里运行外部程序. 该方法有6个可访问版本: 1.exec(String command) 2.exec(String command, String envp[], File dir) 3.exec(String cmd, String envp[]) 4.exec(String cmdarray[]) 5.exec(String cmdarray[], String envp[]) 6.exec(String cmdarray[], String envp[], File dir) 注:向启动的应用程序传多个参数可有一下形式:
1.
Process p=run.exec("E://Myworkspace//wordpanjuan//wordpanjuan//bin//Release//wordpanjuan.exe d://data//27.doc 27");
2.
Process p=run.exec(new String[]{"E://Myworkspace//wordpanjuan//wordpanjuan//bin//Release//wordpanjuan.exe",
"d://data//27.doc","27"}); 一般的应用程序可以直接使用第一版本,当有环境变量传递的时候使用后面的版本. 其中2和6版本可以传递一个目录,标识当前目录,因为有些程序是使用相对目录的,所以就要使用这个版本. 当要执行批处理的时候,不能直接传递批处理的文件名,而要使用: cmd.exe /C start 批处理文件名 使用dos命令(比如dir)时也要使用掉调用. 如果想与调用的程序进行交互,那么就要使用该方法的返回对象Process了,通过Process的getInputStream(),getOutputStream(),getErrorStream()方法可以得到输入输出流,然后通过InputStream可以得到程序对控制台的输出信息,通过OutputStream可以给程序输入指令,这样就达到了程序的交换功能. 例子如下: package com.broadcontact.netadmin.schedule; import java.io.PrintWriter; import java.io.PrintStream; import java.io.IOException; import java.sql.SQLException; import java.util.Date; import java.io.StringReader; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Reader; import java.io.File; import java.io.BufferedWriter; import java.io.OutputStreamWriter; /** * <p>Title: netadmin</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: nm group</p> * @author Maico(Panghf) * @version 1.0 */ public class ExecuteTask implements Runnable { private boolean isRunning=true; public ExecuteTask() { } public void run(){ } public static void main(String[] args){ try { Process proc=Runtime.getRuntime().exec("cmd.exe"); BufferedReader read=new BufferedReader(new InputStreamReader(proc.getInputStream())); new Thread(new Echo(read)).start(); PrintWriter out=new PrintWriter(new OutputStreamWriter(proc.getOutputStream())); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String instr = in.readLine(); while(!"exit".equals(instr)){ instr = in.readLine(); out.println(instr); file://out.println("telnet 192.168.0.1"); out.flush(); } in.readLine(); read.close(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } } class Echo implements Runnable { private BufferedReader read; public Echo(BufferedReader read){ this.read = read; } public void run() { try { String l = read.readLine(); while (l != null) { System.out.println(l); l = read.readLine(); } System.out.println("---执行完毕:"); } catch (IOException ex) { ex.printStackTrace(); } } }
1.
Process p=run.exec("E://Myworkspace//wordpanjuan//wordpanjuan//bin//Release//wordpanjuan.exe d://data//27.doc 27");
2.
Process p=run.exec(new String[]{"E://Myworkspace//wordpanjuan//wordpanjuan//bin//Release//wordpanjuan.exe",
"d://data//27.doc","27"}); 一般的应用程序可以直接使用第一版本,当有环境变量传递的时候使用后面的版本. 其中2和6版本可以传递一个目录,标识当前目录,因为有些程序是使用相对目录的,所以就要使用这个版本. 当要执行批处理的时候,不能直接传递批处理的文件名,而要使用: cmd.exe /C start 批处理文件名 使用dos命令(比如dir)时也要使用掉调用. 如果想与调用的程序进行交互,那么就要使用该方法的返回对象Process了,通过Process的getInputStream(),getOutputStream(),getErrorStream()方法可以得到输入输出流,然后通过InputStream可以得到程序对控制台的输出信息,通过OutputStream可以给程序输入指令,这样就达到了程序的交换功能. 例子如下: package com.broadcontact.netadmin.schedule; import java.io.PrintWriter; import java.io.PrintStream; import java.io.IOException; import java.sql.SQLException; import java.util.Date; import java.io.StringReader; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Reader; import java.io.File; import java.io.BufferedWriter; import java.io.OutputStreamWriter; /** * <p>Title: netadmin</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: nm group</p> * @author Maico(Panghf) * @version 1.0 */ public class ExecuteTask implements Runnable { private boolean isRunning=true; public ExecuteTask() { } public void run(){ } public static void main(String[] args){ try { Process proc=Runtime.getRuntime().exec("cmd.exe"); BufferedReader read=new BufferedReader(new InputStreamReader(proc.getInputStream())); new Thread(new Echo(read)).start(); PrintWriter out=new PrintWriter(new OutputStreamWriter(proc.getOutputStream())); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String instr = in.readLine(); while(!"exit".equals(instr)){ instr = in.readLine(); out.println(instr); file://out.println("telnet 192.168.0.1"); out.flush(); } in.readLine(); read.close(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } } class Echo implements Runnable { private BufferedReader read; public Echo(BufferedReader read){ this.read = read; } public void run() { try { String l = read.readLine(); while (l != null) { System.out.println(l); l = read.readLine(); } System.out.println("---执行完毕:"); } catch (IOException ex) { ex.printStackTrace(); } } }