Java 調用EXE

來源:互聯網
上載者:User

使用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;<br /> import java.io.PrintWriter;<br /> import java.io.PrintStream;<br /> import java.io.IOException;<br /> import java.sql.SQLException;<br /> import java.util.Date;<br /> import java.io.StringReader;<br /> import java.io.BufferedReader;<br /> import java.io.InputStreamReader;<br /> import java.io.Reader;<br /> import java.io.File;<br /> import java.io.BufferedWriter;<br /> import java.io.OutputStreamWriter;<br /> /**<br /> * <p>Title: netadmin</p><br /> * <p>Description: </p><br /> * <p>Copyright: Copyright (c) 2002</p><br /> * <p>Company: nm group</p><br /> * @author Maico(Panghf)<br /> * @version 1.0<br /> */<br /> public class ExecuteTask implements Runnable {<br /> private boolean isRunning=true;<br /> public ExecuteTask() {<br /> }<br /> public void run(){<br /> }<br /> public static void main(String[] args){<br /> try {<br /> Process proc=Runtime.getRuntime().exec("cmd.exe");<br /> BufferedReader read=new BufferedReader(new InputStreamReader(proc.getInputStream()));<br /> new Thread(new Echo(read)).start();<br /> PrintWriter out=new PrintWriter(new OutputStreamWriter(proc.getOutputStream()));<br /> BufferedReader in=new BufferedReader(new InputStreamReader(System.in));<br /> String instr = in.readLine();<br /> while(!"exit".equals(instr)){<br /> instr = in.readLine();<br /> out.println(instr);<br /> file://out.println("telnet 192.168.0.1");<br /> out.flush();<br /> }<br /> in.readLine();<br /> read.close();<br /> out.close();<br /> }<br /> catch (IOException ex) {<br /> ex.printStackTrace();<br /> }<br /> }<br /> }<br /> class Echo implements Runnable {<br /> private BufferedReader read;<br /> public Echo(BufferedReader read){<br /> this.read = read;<br /> }<br /> public void run() {<br /> try {<br /> String l = read.readLine();<br /> while (l != null) {<br /> System.out.println(l);<br /> l = read.readLine();<br /> }<br /> System.out.println("---執行完畢:");<br /> }<br /> catch (IOException ex) {<br /> ex.printStackTrace();<br /> }<br /> }<br /> }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.