请问在java中如何调用其它的应用程序

解决方案 »

  1.   

    利用runtime类的exec()方法能用来执行外部程序!
    比如说用该方法可以打开一个网页!
      

  2.   

    Runtime运行外部程序的小结- -                           
    使用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)一般的应用程序可以直接使用第一版本,当有环境变量传递的时候使用后面的版本.
    其中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();
        }
      }
    }网上关于runtime的知识很多,楼主可以自己去查一下·
      

  3.   

    一个最简单的例子,打开一个网页
    public class hello
    {
    public static void main (String [] args)
    {
    try{

    Process p=Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/iexplore.exe http://www.163.com");
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }