java执行adb命令

public static void main(String[] args) {
        String adbHome="/Users/xiaowen/Library/Android/sdk/platform-tools/";
        String cmd=adbHome+"adb version";
        Process process;
        try {
            process=Runtime.getRuntime().exec(cmd);
            System.out.println(InputStream2String(process.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String InputStream2String(InputStream inputStream){
        String result="";
        BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
        try {
            String temp="";
            while ((temp=br.readLine())!=null){
                result+=temp+"\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

输出:
Android Debug Bridge version 1.0.39
Version 27.0.0-4455170
Installed as /Users/xiaowen/Library/Android/sdk/platform-tools/adb

adbHome 是adb文件所在路径

java虚拟机加载的时候没有adb相关的环境变量,如果执行adb的命令,需要使用全路径。

你可能感兴趣的:(java执行adb命令)