Java:运行多个 shell 命令?
好的。我一直在到处寻找如何在 java 的单个命令提示符上执行多个命令。我需要做的是这个,但不是在命令行中,而是在代码中。
执行:
cd C:/Android/SDK/platform-tools
adb install superuser.apk
..基本上我想从程序运行adb命令!到目前为止,这是我的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
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();
}
}
}
那么发生的事情是......我可以运行第一个命令,但是该cmd终止,当我执行第二个命令时,会创建一个新的cmd,因此我得到一个错误,因为我不在正确的目录中。我尝试了单个字符串命令“cmd /C cd C:/blablabla /C adb remount”,但这只是冻结了......
本质上,命令 1 被执行并终止,然后命令 2 被执行并终止。我希望它是这样的:命令 1 执行,命令 2 执行,终止。
基本上我问的是如何在一个命令提示符上连续运行这两个命令???
我的最终目标是拥有一个带有一堆按钮的 JFrame,这些按钮在点击。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是制作一个批处理文件,然后从程序中调用它
当然,您可以说,
如果直接命名文件,则无需 cd 到文件,
尽管您要查找的内容已在 ddms.bat 中生成,它提供了到 adb 的完整可视链接
Easiest way is to make a batch file then call that from program
of course you could just say
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
创建文件
something.bat
并将内容设置为:然后调用:
执行 bat 文件中的所有命令。
Create file as
something.bat
and set the contents to:Then call:
all commands in the bat file are executed.