OK. I've been looking everywhere on how to execute multiple commands on a single command prompt from java. What i need to do is this, but not in command line, in code.
Execute:
cd C:/Android/SDK/platform-tools
adb install superuser.apk
..Basically i want to run adb commands from a program!!! Here is my java code so far:
MainProgram.java
public class MainProgram {
public static void main(String[] args) {
CMD shell = new CMD();
shell.execute("cmd /K cd C:/Android/SDK/platform-tools"); //command 1
shell.execute("cmd /C adb install vending.apk"); // command 2
}
}
CMD.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CMD {
CMD() {
}
// THIS METHOD IS WHERE THE PROBLEM IS
void execute(String command) {
try
{
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
String 开发者_运维知识库s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
So what happens is...i can run the first command, but that cmd terminates and when i execute the 2nd command, a new cmd is created, hence i get an error because im not in the right directory. I tried a single string command "cmd /C cd C:/blablabla /C adb remount", but that just froze up...
Essentially, command 1 is executed and terminated, then command 2 is executed and terminated. I want it to be like this: command 1 executed, command 2 executed, terminated.
Basically i'm asking how can i run both of these commands in a row on a single command prompt???
My final target is to have a JFrame with a bunch of buttons which execute different adb commands when clicked on.
Easiest way is to make a batch file then call that from program of course you could just say
C:/Android/SDK/platform-tools/adb install superuser.apk
there's no need to cd to a file if you name it directly
although what you are looking for is already made in ddms.bat which provides a complete visual link to adb
Create file as something.bat
and set the contents to:
cd C:/Android/SDK/platform-tools
adb install superuser.apk
Then call:
Process p = Runtime.getRuntime().exec("something.bat");
all commands in the bat file are executed.
精彩评论