简体   繁体   English

Java:运行多个Shell命令?

[英]Java: Run multiple shell commands?

OK. 好。 I've been looking everywhere on how to execute multiple commands on a single command prompt from java. 我到处都在寻找如何从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!!! ..基本上我想从程序中运行adb命令!!! Here is my java code so far: 到目前为止,这是我的Java代码:

MainProgram.java 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 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. 所以发生的是...我可以运行第一个命令,但是该cmd终止,当我执行第二个命令时,会创建一个新的cmd,因此由于im不在正确的目录中,因此我得到了一个错误。 I tried a single string command "cmd /C cd C:/blablabla /C adb remount", but that just froze up... 我尝试了单个字符串命令“ cmd / C cd C:/ blablabla / C adb remount”,但是那只是冻结了...

Essentially, command 1 is executed and terminated, then command 2 is executed and terminated. 本质上,命令1被执行并终止,然后命令2被执行并终止。 I want it to be like this: command 1 executed, command 2 executed, terminated. 我希望它是这样的:执行命令1,执行命令2,终止。

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. 我的最终目标是拥有一个带有一堆按钮的JFrame,这些按钮在单击时会执行不同的adb命令。

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 如果直接命名,则无需CD到文件

although what you are looking for is already made in ddms.bat which provides a complete visual link to adb 尽管您正在寻找的东西已经在ddms.bat中完成,它提供了到adb的完整可视链接

Create file as something.bat and set the contents to: 将文件创建为something.bat并将内容设置为:

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. bat文件中的所有命令都将执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM