最近,我在nano ./bash_profile中添加了“adb devices”,以便我可以从任何目录运行它。 我用一个Java应用程序运行来自java应用程序的cmd命令错误
public static void main(String [] args) {
executeCmd("adb devices");
}
private static void executeCmd(String string) {
InputStream pipedOut = null;
try {
Process aProcess = Runtime.getRuntime().exec(string);
// These two thread shall stop by themself when the process end
Thread pipeThread = new Thread(new StreamGobber(aProcess.getInputStream()));
Thread errorThread = new Thread(new StreamGobber(aProcess.getErrorStream()));
pipeThread.start();
errorThread.start();
aProcess.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
class StreamGobber implements Runnable {
private InputStream Pipe;
public StreamGobber(InputStream pipe) {
if(pipe == null) {
throw new NullPointerException("bad pipe");
}
Pipe = pipe;
}
public void run() {
try {
byte buffer[] = new byte[2048];
int read = Pipe.read(buffer);
while(read >= 0) {
System.out.write(buffer, 0, read);
read = Pipe.read(buffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(Pipe != null) {
try {
Pipe.close();
} catch (IOException e) {
}
}
}
当我运行任何其他命令,如“LS”它的正常工作! 我使用的是Mac ..在Mac
谢谢:)
这是什么问题? – Apurva 2015-03-19 08:29:51
如果你可以运行命令,你在终端上没有检查你的Java应用程序吗?并且指定您得到的错误/应用程序的功能以及您期望的功能。 – dehlen 2015-03-19 08:32:01
嗨unverschaemt,是的,我检查了它,它运行并列出终端中可用的设备。这是我得到的错误: java.io.IOException:无法运行程序“adb”:error = 2,没有这样的文件或目录 \t at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042) \t at java.lang.Runtime.exec(Runtime.java:620) \t在java.lang.Runtime.exec(Runtime.java:450) \t在java.lang.Runtime.exec(Runtime.java:347) \t Execution.executeCmd(Execution.java:44) \t at Execution.main(Execution.java:37) – Omarkk 2015-03-19 08:43:29