Java 调用第3方系统服务

Java 调用windows 或 shell脚本服务

package com.zhiwei.basic;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**java调用windows本地命令*/
public class DosOperation {

    public static void main(String[] args) {
        
        InputStream is=null;
        BufferedInputStream bis=null;
        Process process=null;
        
        /**命令参数*/
        String[] cmd=new String[]{"cmd.exe","/c","dir"};
        
        try{
            
            process=Runtime.getRuntime().exec(cmd);
            is=process.getInputStream();
            bis=new BufferedInputStream(is);
            byte[] buf=new byte[1024];
            while((bis.read(buf))!=-1){
                System.out.println(new String(buf,"GBK"));
            }
            
            int exitValue=process.waitFor();
            System.out.println("返回值:"+exitValue);
            
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(bis!=null){
                bis.close();
                }
                if(process!=null){
                    process.getOutputStream().close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

###结果:

--------------------------------------------------------------------------
 驱动器 E 中的卷是 Entainment
 卷的序列号是 000B-75D9

 E:\JavaProject\JavaPro 的目录

2016/11/28  11:07    <DIR>          .
2016/11/28  11:07    <DIR>          ..
2016/11/28  10:36             1,759 .classpath
2016/05/25  14:22               383 .project
2016/09/26  23:45    <DIR>          .settings
2016/12/05  10:57    <DIR>          bin
2016/11/24  11:42    <DIR>          lib
2016/11/28  12:10    <DIR>          src
               2 个文件          2,142 字节
               6 个目录 145,481,375,744 可用字节
返回值:0

转载于:https://my.oschina.net/yangzhiwei256/blog/3017452

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值