<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    大夢(mèng)想家

    5年開發(fā)工程師,2年實(shí)施經(jīng)理,X年售前顧問,......
    數(shù)據(jù)加載中……
    如何用java啟動(dòng)windows命令行程序

    先請(qǐng)編譯和運(yùn)行下面程序:
    import java.util.*;
    import java.io.*;
    public class BadExecJavac2
    {
        public static void main(String args[])
        {
            try
            {            
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec("javac");
                int exitVal = proc.waitFor();
                System.out.println("Process exitValue: " + exitVal);
            } catch (Throwable t){
                t.printStackTrace();
            }
        }
    }

       我們知道javac命令,當(dāng)不帶參數(shù)運(yùn)行javac 程序時(shí),它將輸出幫助說明,為什么上面程序不產(chǎn)生任何輸出并掛起,永不完成呢?java文檔上說,由于有些本地平臺(tái)為標(biāo)準(zhǔn)輸入和輸出流所提供的緩沖區(qū)大小有限,如果不能及時(shí)寫入子進(jìn)程的輸入流或者讀取子進(jìn)程的輸出流,可能導(dǎo)致子進(jìn)程阻塞,甚至陷入死鎖。所以,上面的程序應(yīng)改寫為:
    import java.util.*;
    import java.io.*;
    public class MediocreExecJavac
    {
        public static void main(String args[])
        {
            try
            {            
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec("javac");
                InputStream stderr = proc.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                System.out.println("<ERROR>");
                while ( (line = br.readLine()) != null)
                    System.out.println(line);
                System.out.println("</ERROR>");
                int exitVal = proc.waitFor();
                System.out.println("Process exitValue: " + exitVal);
            } catch (Throwable t){
                t.printStackTrace();
            }
        }
    }
    下面是正確的輸出:
    D:\java>java   MediocreExecJavac
    <ERROR>
    Usage: javac <options> <source files>
    where possible options include:
      -g                         Generate all debugging info
      -g:none                    Generate no debugging info
      -g:{lines,vars,source}     Generate only some debugging info
      -nowarn                    Generate no warnings
      -verbose                   Output messages about what the compiler is doing
      -deprecation               Output source locations where deprecated APIs are used
      -classpath <path>          Specify where to find user class files
      -cp <path>                 Specify where to find user class files
      -sourcepath <path>         Specify where to find input source files
      -bootclasspath <path>      Override location of bootstrap class files
      -extdirs <dirs>            Override location of installed extensions
      -endorseddirs <dirs>       Override location of endorsed standards path
      -d <directory>             Specify where to place generated class files
      -encoding <encoding>       Specify character encoding used by source files
      -source <release>          Provide source compatibility with specified release
      -target <release>          Generate class files for specific VM version
      -version                   Version information
      -help                      Print a synopsis of standard options
      -X                         Print a synopsis of nonstandard options
      -J<flag>                   Pass <flag> directly to the runtime system
    </ERROR>
    Process exitValue: 2
    D:\java>
       下面是一個(gè)更一般的程序,它用兩個(gè)線程同步清空標(biāo)準(zhǔn)錯(cuò)誤流和標(biāo)準(zhǔn)輸出流,并能根據(jù)你所使用的windows操作系統(tǒng)選擇windows命令解釋器command.com或cmd.exe,然后執(zhí)行你提供的命令。
    import java.util.*;
    import java.io.*;
    class StreamGobbler extends Thread
    {
        InputStream is;
        String type;  //輸出流的類型ERROR或OUTPUT
        StreamGobbler(InputStream is, String type)
        {
            this.is = is;
            this.type = type;
        }
        public void run()
        {
            try
            {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line=null;
                while ( (line = br.readLine()) != null)
                {
                    System.out.println(type + ">" + line);
                    System.out.flush();
                }
                } catch (IOException ioe)
                  {
                    ioe.printStackTrace();  
                  }
        }
    }
    public class GoodWindowsExec
    {
        public static void main(String args[])
        {
            if (args.length < 1)
            {
                System.out.println("USAGE: java GoodWindowsExec <cmd>");
                System.exit(1);
            }
            try
            {            
                String osName = System.getProperty("os.name" );
                System.out.println("osName: " + osName);
                String[] cmd = new String[3];
                if(osName.equals("Windows XP") ||osName.equals("Windows 2000"))
                {
                    cmd[0] = "cmd.exe" ;
                    cmd[1] = "/C" ;
                    cmd[2] = args[0];
                }
                else if( osName.equals( "Windows 98" ) )
                {
                    cmd[0] = "command.com" ;
                    cmd[1] = "/C" ;
                    cmd[2] = args[0];
                }
                Runtime rt = Runtime.getRuntime();
                System.out.println("Execing " + cmd[0] + " " + cmd[1]+ " " + cmd[2]);
                Process proc = rt.exec(cmd);
                // any error message?
                StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");       
                // any output?
                StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
                // kick them off
                errorGobbler.start();
                outputGobbler.start();
                // any error???
                int exitVal = proc.waitFor();
                System.out.println("ExitValue: " + exitVal);
            } catch (Throwable t){
                t.printStackTrace();
            }
        }
    }
    下面是一個(gè)測(cè)試結(jié)果:
    D:\java>java  GoodWindowsExec "copy Test.java Test1.java"
    osName: Windows XP
    Execing cmd.exe /C copy Test.java Test1.java
    OUTPUT>已復(fù)制         1 個(gè)文件。
    ExitValue: 0
    D:\java>
    下面的測(cè)試都能通過(windows xp+jdk1.5)
    D:\java>java   GoodWindowsExec dir
    D:\java>java   GoodWindowsExec Test.java
    D:\java>java   GoodWindowsExec regedit.exe
    D:\java>java   GoodWindowsExec NOTEPAD.EXE
    D:\java>java   GoodWindowsExec first.ppt
    D:\java>java   GoodWindowsExec second.doc



    客戶虐我千百遍,我待客戶如初戀!

    posted on 2007-12-26 13:10 阿南 閱讀(738) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲精品中文字幕无码AV| 24小时免费看片| 亚洲第一第二第三第四第五第六 | 亚洲精品第五页中文字幕| 亚洲成a人片在线观看久| 国产卡一卡二卡三免费入口| 天堂在线免费观看| 男女交性无遮挡免费视频| 456亚洲人成在线播放网站| 亚洲一区二区在线免费观看| 亚洲国产精品成人久久蜜臀| 日韩精品免费电影| 九九九精品成人免费视频| 最近中文字幕完整免费视频ww| 巨胸喷奶水www永久免费| 国产亚洲精品欧洲在线观看| 亚洲国产激情在线一区| 亚洲国产综合第一精品小说| 中文字幕亚洲第一在线| 无码欧精品亚洲日韩一区| 亚洲乱码中文字幕久久孕妇黑人| 亚洲情侣偷拍精品| 亚洲男人的天堂在线va拉文| 亚洲高清无码综合性爱视频| 国产伦精品一区二区三区免费迷| 日韩在线视频免费看| 精品无码国产污污污免费| 美女黄网站人色视频免费国产 | 久久av无码专区亚洲av桃花岛| 久久久无码精品亚洲日韩蜜桃 | 国产猛男猛女超爽免费视频| 一本到卡二卡三卡免费高| 男男gay做爽爽的视频免费| 香蕉视频亚洲一级| 视频一区在线免费观看| 羞羞视频免费网站入口| 国产成人va亚洲电影| 瑟瑟网站免费网站入口| 国产黄色片免费看| a毛片在线免费观看| 久久久久久国产精品免费免费男同 |