2

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?

1
  • What are you expecting to happen after cd ? Do you mean 'shell ls /data/app'? Commented Oct 16, 2011 at 3:22

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

adb is a client and it runs in a host . if you try to execute adb in a android shell it will produce an error.
0

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

String cmd = "ls";

2 Comments

I means I have a java program run in pc which will triger the adb commend. like the cmd =adkHome + "adb.exe -s " + device + " shell". Then I need to follow up by another commends which try to run in the shell adb opened. like "ls" and "cd "...
Ok - I don't think you can do what you want then. As far as I understand it, each adb.exe command starts a new shell so even if you 'cd data/app' in one shell, it won't preserve that for the next. If you are just trying to ls a specific directory, you could do as @dtmilano suggested and issue a fuller command 'ls /data/app'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.