susantjs
susantjs

Reputation: 939

How to write adb shell commend in java.lang.Runtime.getRuntime()?

I need to use adb connect pc to device and do some checking. So I try to use java.lang.Runtime.getRuntime().exec(cmd) to get adb shell result into my program. But I don't know how to write the adb shell commend in the exec call, something like:

String cmd =adkHome + "adb.exe -s " + device + " shell ls";

then cd data/app

How do I do this?

Upvotes: 2

Views: 9374

Answers (2)

Isaac Parker
Isaac Parker

Reputation: 27

This may be what you're looking for?

    
    String cmd = "adb shell ls";
    String cmdreturn = "";
    Runtime run = Runtime.getRuntime();
    Process pr = run.exec(cmd);
    pr.waitFor();
    BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    while ((line=buf.readLine())!=null) {
         System.out.println(cmdreturn);
    }
    
    

As far as preforming actions in a shell, I would recommend writing a shell script that you execute. In this case instead of

    
    String cmd = "adb shell ls";
    
    
Replace it with
    
    String cmd = "shellscript.sh";
    
    

Cheers!

Upvotes: 1

brianestey
brianestey

Reputation: 8302

If you mean to run this on the phone from your java app, you just need:

String cmd = "ls";

Upvotes: 0

Related Questions