This sample will execute the App:NICConfig.exe under Disk D. (陽春版)
- package gays.runtime;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class CallingCMD_Sample {
- public static void main(String args[]){
- StringBuffer commandLine = new StringBuffer("D:\\NICConfig.exe \"LocalNetwork\" STATUS");
- //StringBuffer commandLine = new StringBuffer("cmd");
- try{
- Runtime rt = Runtime.getRuntime();
- Process p = rt.exec(commandLine.toString());
- BufferedReader input = new BufferedReader(new InputStreamReader(p
- .getInputStream()));
- String line=null;
- while ((line = input.readLine()) != null) {
- System.out.println(line);
- }
- input.close();
- System.out.print("========================================================================");
- }catch(IOException ioe){
- ioe.printStackTrace();
- }
- }
- }
- _StreamGobbler.java 代碼 : 使用來處理 Stderr/ Std 的輸出
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package flib.util.inner;
- import flib.util.inner.enums.EStreamType;
- import java.io.*;
- /**
- *
- * @author John-Lee
- */
- public class _StreamGobbler extends Thread {
- InputStream is;
- EStreamType type;
- private boolean isShow = true;
- public _StreamGobbler(InputStream is, EStreamType type) {
- this.is = is;
- this.type = type;
- }
- public _StreamGobbler(InputStream is, EStreamType type, boolean isShow) {
- this(is, type);
- this.isShow = isShow;
- }
- public void run() {
- try {
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line = null;
- while ((line = br.readLine()) != null) {
- if(isShow) {
- System.out.println(type.toString() + ">" + line);
- }
- System.out.flush();
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- /**
- * @return the isShow
- */
- public boolean isIsShow() {
- return isShow;
- }
- /**
- * @param isShow the isShow to set
- */
- public void setIsShow(boolean isShow) {
- this.isShow = isShow;
- }
- }
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package flib.util.inner.enums;
- /**
- *
- * @author John-Lee
- */
- public enum EStreamType {
- OUTPUT("STDOUT"),ERROR("STDERR");
- private String msg;
- private EStreamType(String m) {
- msg = m;
- }
- public String toString() {
- return msg;
- }
- public String getMsg() {
- return msg;
- }
- }
- package ts.test;
- import java.io.BufferedReader;
- import java.io.IOException;
- public class Test3 {
- public static void main(String args[]) {
- try {
- String cmdline = "ipconfig";
- boolean isShow = true;
- StringBuffer res = new StringBuffer("");
- Runtime runtime = Runtime.getRuntime();
- Process process = null;
- BufferedReader sb = null;
- process = runtime.exec(cmdline);
- // any error message?
- _StreamGobbler errorGobbler = new _StreamGobbler(process.getErrorStream(), EStreamType.ERROR, isShow);
- // any output?
- _StreamGobbler outputGobbler = new _StreamGobbler(process.getInputStream(), EStreamType.OUTPUT, isShow);
- // kick them off
- errorGobbler.start();
- outputGobbler.start();
- process.getOutputStream().flush();
- process.getOutputStream().close();
- int exitCode = process.waitFor();
- //System.out.println("ExitValue: " + exitVal);
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
* java 調用 Windows 下 DOS 命令 wmic 奇怪現象
* Java編程 - WMIC的和Java
沒有留言:
張貼留言