JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:java中呼叫.bat 卻沒有反應.... [Re:JCChen]
ryanpai





發文: 47
積分: 0
於 2007-05-17 22:16 user profilesend a private message to usersend email to ryanpaireply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
試看看吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
              String[] cmd = new String[3];
              Runtime rt = Runtime.getRuntime();
              cmd[0] = "cmd.exe" ;
              cmd[1] = "/C" ;
              cmd[2] = "D:\\TWDATA\\CTL\\YourBatchFile.bat" ; //修改這裡
                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);               


StreamGobbler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    
    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);    
            } catch (IOException ioe)
              {
                ioe.printStackTrace();  
              }
    }
}


ryanpai edited on 2007-05-18 12:16
reply to postreply to post
Ryan Pai
話題樹型展開
人氣 標題 作者 字數 發文時間
13569 java中呼叫.bat 卻沒有反應.... JCChen 795 2007-05-15 15:39
12347 Re:java中呼叫.bat 卻沒有反應.... JCChen 276 2007-05-16 16:08
12010 Re:java中呼叫.bat 卻沒有反應.... JCChen 23 2007-05-17 11:19
12167 Re:java中呼叫.bat 卻沒有反應.... ryanpai 144 2007-05-17 12:29
11988 Re:java中呼叫.bat 卻沒有反應.... JCChen 260 2007-05-17 13:58
11988 Re:java中呼叫.bat 卻沒有反應.... Duncan 721 2007-05-17 18:46
13532 Re:java中呼叫.bat 卻沒有反應.... ryanpai 1532 2007-05-17 22:16
12453 Re:java中呼叫.bat 卻沒有反應.... ok12360000 556 2007-09-19 09:51
12085 Re:java中呼叫.bat 卻沒有反應.... ok12360000 416 2007-09-19 11:14
11358 Re:java中呼叫.bat 卻沒有反應.... ok12360000 104 2007-09-15 11:32
11346 Re:java中呼叫.bat 卻沒有反應.... ryanpai 1226 2007-09-17 13:40
11293 Re:java中呼叫.bat 卻沒有反應.... ryanpai 71 2007-09-21 00:27
» JWorld@TW »  Java 新手區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8