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'?CommentedOct 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!

1
  • 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.CommentedJul 4, 2018 at 16:34
0

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

String cmd = "ls"; 
2
  • 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 "...
    – susantjs
    CommentedOct 17, 2011 at 2:32
  • 1
    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'.CommentedOct 17, 2011 at 9:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.