如何通过Java文件运行adb命令? [英] How to run an adb command through a Java file?

查看:159
本文介绍了如何通过Java文件运行adb命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的代码,无法完全解决该错误。不知道这些信息是否有用,但是我使用的是Mac,并且使用的是IntelliJ编辑器。

I have written the following code and cannot quite figure out how to solve the error. Not sure if this information will be found useful, but I am using a Mac and using the editor IntelliJ.

public class TestCode {
    public static void main(String[] args) throws Exception {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("adb devices");
    }
}

结果为线程 main中的异常 java .io.IOException:无法运行程序 adb:error = 2,没有此类文件或目录

The result is "Exception in thread "main" java.io.IOException: Cannot run program "adb": error=2, No such file or directory"

但是,当我从命令行运行命令 adb devicees时终端,我会获得连接到计算机的设备列表。

However, when I run the command "adb devicees" from the terminal I get the list of devices attached to my computer.

对于那些感兴趣的人,以下是完整的堆栈跟踪。

For those interested, the following is the full stack trace.

Exception in thread "main" java.io.IOException: Cannot run program "adb": error=2, No such file or directory
    at java.lang.ProcessBuilder.processException(ProcessBuilder.java:478)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:457)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at java.lang.Runtime.exec(Runtime.java:328)
    at com.sonos.acr.test.TestCode.main(TestCode.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
    at java.lang.ProcessImpl.start(ProcessImpl.java:91)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
    ... 9 more

在此先感谢您的建议,建议和/或帮助。

Thank you in advance for suggestions, advice, and/or help.

推荐答案

您需要使用 exec(String [] cmdarray)函数发送参数,该函数的单个参数版本会在空间上拆分字符串,如果您的路径存在,则会带来麻烦包含空格。

You need to use the exec(String[] cmdarray) function to send arguments, the single argument version of the function splits the string on space, and that spells trouble if your path contains spaces.

您还需要指定完整路径(也许是/ usr / bin / adb?)。

you also need to specify the full path (maybe /usr/bin/adb?).

就像这样:

Process process = runtime.exec(new String[] {"/usr/bin/adb", "devices"});

这篇关于如何通过Java文件运行adb命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆