免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 950 | 回复: 0
打印 上一主题 下一主题

Java执行shell [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-12 11:43 |只看该作者 |倒序浏览
CODE: jShell.java
----------------------------------------------------------------------------------
/*
*  jShell.java
*  class jShell is used for executing shell command
*  USAGE:
*      jShell obj=new jShell(shellCommand);
*      obj.startErr();
*      obj.startOut();
*      obj.startIn();
*  You can Interupt I/O thread when nessasary:
*      obj.interruptErr();
*      obj.interruptOut();
*      obj.interruptIn();
*  
*  BY Ahui Wang    Nankai U.    2007-05-12   
*/
import java.io.*;
public class jShell {
    Thread tIn;    //handle input of child process
    Thread tOut;//handle output of child process
    Thread tErr;//handle error output of child process
   
    public jShell(String shellCommand){
        
        Process child=null;    //child process
        try{
            child=Runtime.getRuntime().exec(shellCommand);
        }
        catch(IOException e){
            e.printStackTrace();
        }
        if(child==null){
            return;
        }
        
        final InputStream inputStream=child.getInputStream();
        final BufferedReader brOut=
            new BufferedReader(new InputStreamReader(inputStream));
            
        tOut=new Thread(){    //initialize thread tOut
            String line;
            int lineNumber=0;
            public void run(){
                try{
                    while((line=brOut.readLine())!=null){   
                        System.out.println(lineNumber+". "+line);
                        lineNumber++;
                        }
                    }
                catch(IOException e){
                    e.printStackTrace();
                }
            }
        };
            
        final InputStream errorStream=child.getErrorStream();
        final BufferedReader brErr=
            new BufferedReader(new InputStreamReader(errorStream));
        
        tErr=new Thread(){    //initialize thread tErr
            String line;
            int lineNumber=0;
            public void run(){
                try{
                    while((line=brErr.readLine())!=null){
                        System.out.println(lineNumber+". "+line);
                        lineNumber++;
                        }
                    }
                catch(IOException e){
                    e.printStackTrace();
                }
            }
        };
        
        // read buffer of parent process' input stream
        final BufferedReader reader =
            new BufferedReader(new InputStreamReader(System.in));
        final OutputStream outputStream = child.getOutputStream();
        tIn=new Thread(){
            String line;
            public void run() {
                try {
                    while (true) {
                        outputStream.write( (reader.readLine()+"\n").getBytes());
                        outputStream.flush();
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
            
    }
    public void startIn(){ //start thread tIn
        if(tIn!=null){
            tIn.start();
        }
    }
    public void startErr(){ //start thread tErr
        if(tErr!=null){
            tErr.start();
        }
    }
    public void startOut(){ //start thread tOut
        if(tOut!=null){
            tOut.start();
        }
    }
    public void interruptIn(){ //interrupt thread tIn
        if(tIn!=null){
            tIn.interrupt();
        }
    }
    public void interruptErr(){ //interrupt thread tErr
        if(tErr!=null){
            tErr.interrupt();
        }
    }
    public void interruptOut(){ //interrupt thread tOut
        if(tOut!=null){
            tOut.interrupt();
        }
    }
   
}
CODE: mainC.java
----------------------------------------------------------------------------------
public final class mainC {
    public static void main(String[] args) {
        jShell shell=new jShell("ls -l");
        shell.startErr();
        shell.startIn();
        shell.startOut();
    }
}
RESULT:
---------------------------------------------------------------------------------
0. 总用量 44
1. -rwxrwxrwx    1 root     root          219  5月 12 10:41 ex.pl
2. -rwxrwxrwx    1 root     root          211  5月 12 10:39 ex.pl~
3. -rwxrwxrwx    1 root     root          150  5月 12 10:41 ex.sh
4. -rwxrwxrwx    1 root     root          124  5月 12 10:20 ex.sh~
5. -rwxrwxrwx    1 root     root         1198  5月 12 10:43 jShell$1.class
6. -rwxrwxrwx    1 root     root         1198  5月 12 10:43 jShell$2.class
7. -rwxrwxrwx    1 root     root         1222  5月 12 10:43 jShell$3.class
8. -rwxrwxrwx    1 root     root         2241  5月 12 10:43 jShell.class
9. -rwxrwxrwx    1 root     root         2720  5月 12 10:43 jShell.java
10. -rwxrwxrwx    1 root     root          544  5月 12 11:43 mainC.class
11. -rwxrwxrwx    1 root     root          170  5月 12 11:43 mainC.java


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/37897/showart_299258.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP