Java执行window命令和linux命令

这里强调一点,命令中用到的jdk还有其他一些需要配置的软件的环境变量一定要配置成全局的,外部可以读取的,否则java根本无法执行命令。比如linux需要配置在 /etc/profile,用source /etc/profile可以即时生效。

下面是代码:

package com.apk.openUser.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


public class AddLinuxUtil {
    //命令之间都是用 && 进行连接,如果命令多,可以写成可执行的脚本。
	//linux 命令的执行,方式1
	public static List<String> executeNewFlow(List<String> commands) {
        List<String> rspList = new ArrayList<String>();
        Runtime run = Runtime.getRuntime();
        try {
            Process proc = run.exec("/bin/bash", null, null);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
            for (String line : commands) {
                out.println(line);
            }
            // out.println("cd /home/test");
            // out.println("pwd");
            // out.println("rm -fr /home/proxy.log");
            out.println("exit");// 这个命令必须执行,否则in流不结束。
            String rspLine = "";
            while ((rspLine = in.readLine()) != null) {
                System.out.println(rspLine);
                rspList.add(rspLine);
                if (rspLine.equals("BUILD SUCCESSFUL")) {
                    System.out.println("成功");
                }
            }
            proc.waitFor();
            in.close();
            out.close();
            proc.destroy();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return rspList;
    }
	//方式二:
	public static int executeNewFlow(String bf,String af,String app) {
		//System.out.println(commands);
        
        Runtime run = Runtime.getRuntime();
        int iden = 0;
        //String[] cmds = {"/bin/sh", "-c", commands};
        String cmds = "cp "+bf+" -vf "+af+" && "+app;
        BufferedReader in = null;
        //BufferedReader er = null;//输出执行错误的命令内容
        try {
             Process proc = run.exec(new String[]{"/bin/sh","-c",cmds});
        	//Process proc = run.exec(cmds);
            in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            //er = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String rspLine = "";
            StringBuffer b=new StringBuffer();
            while ((rspLine = in.readLine()) != null) {
                System.out.println(rspLine);
                b.append(rspLine+"\n");
                if (rspLine.indexOf("SUCCESSFUL") != -1) {
                    System.out.println("成功");
                    iden = 1;
                }
            }
            String rline = null;
            System.out.println(b.toString());
            /*while ((rline=er.readLine())!=null) {
                System.out.println("error::::"+rline);
                
                
            }*/
            proc.waitFor();
            
        } catch (Exception e1) {
            e1.printStackTrace();
        }finally {
        	if (in != null) {
        		try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        	}
        }
        return iden;
    }
	//windows命令的执行
	
	public static int exeCmd(String cmd,String apath,String bpath) {
        Runtime runtime = Runtime.getRuntime();
        BufferedReader br = null;
        int iden = 0;//未打包成功
        
     
        String cmd1 = "cmd /c replace "+apath+" "+bpath+" /s && "+cmd;
        System.out.println("cmd:"+cmd1);
        
        try {
        	Process pro = runtime.exec(cmd1);
            br = new BufferedReader(new InputStreamReader(pro.getInputStream(),"gbk"));
            //StringBuffer b = new StringBuffer();
            String line=null;
            StringBuffer b=new StringBuffer();
            while ((line=br.readLine())!=null) {
                b.append(line+"\n");
                System.out.println(line);
                if (line.indexOf("SUCCESSFUL") != -1) {
                    System.out.println("成功");
                    iden = 1;
                }
                
                
            }
            System.out.println(b.toString());
                 
	        pro.waitFor();   
	        
	        
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
        	if (br != null) {
        		try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
        	}
        	
        }
        return iden;
	}
	public static void main(String[] args) {
	    /*String[] it = {"D:","cd androidwork\\weptt-text","gradle build"};//&& cd androidwork\\weptt-text && gradle build
	    exec(it);*/
//		String cmd = "java -version";// D:\\androidwork\\weptt-text && gradle build";
//        
//        String mCurrentPath = System.getProperty("user.dir");
//        String mCurrentDrive = mCurrentPath.substring(0, 2);
        
        String cmdCode = "cd D:\\androidwork\\weptt && gradle clean build ";
        String apath = "D:\\userCustom\\81111\\ptt_config.xml";
        String bpath = "D:\\androidwork\\weptt\\WepttApp\\src\\main\\res\\menu";
        System.out.println("打印Code: >>> "+cmdCode);
        int m = exeCmd(cmdCode,apath,bpath);
        System.out.println("mm:"+m);
        //TimingSchedule tl = new  TimingSchedule();
        //tl.temp1();

	}
	
	
	

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值