2015-03-19 57 views
0

最近,我在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

谢谢:)

+0

这是什么问题? – Apurva 2015-03-19 08:29:51

+0

如果你可以运行命令,你在终端上没有检查你的Java应用程序吗?并且指定您得到的错误/应用程序的功能以及您期望的功能。 – dehlen 2015-03-19 08:32:01

+0

嗨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

回答

0

也许全局路径问题。您可以尝试以绝对adb程序路径作为命令运行。

+0

你的意思是把:Process aProcess = Runtime.getRuntime()。exec(“cd/Volumes/development/android-sdk-macosx/tools adb devices”); ???没有光盘的 – Omarkk 2015-03-19 08:55:04

+0

。我没有Mac,但在Linux上我会用这样的东西:“/ opt/mydevtools/android-sdk/tools/bin/adb”,它的正义示例路径我不记得adb执行bin文件的位置。 – 2015-03-19 09:02:46

+0

我试过没有CD,没有错误,但没有结果和回应。设备已连接并出现在终端中。 – Omarkk 2015-03-19 09:13:22