Error executing cmd to get Android AVD's list from java program running on Windows

huangapple go评论58阅读模式
英文:

Error executing cmd to get Android AVD's list from java program running on Windows

问题

我尝试通过执行以下代码来获取我的计算机上安装的AVD列表:

try {

    String exeCmd = sdkPath + "/tools/bin/avdmanager list avd";

    Process process = Runtime.getRuntime().exec(exeCmd);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

} catch (IOException e) {
    e.printStackTrace();
}

在运行此代码后,我读取输入并查看是否有模拟器可供启动。

这段代码在Mac上执行程序时运行正常,但在Windows上运行时会抛出IOException,如下所示:

java.io.IOException: Cannot run program "C:\Users\Myname\AppData\Local\Android\Sdk/tools/bin/avdmanager": CreateProcess error=2, The system cannot find the file specified (...)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:444)
    at java.lang.ProcessImpl.start(ProcessImpl.java:139)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 7 more

有人能帮助我解决这个问题或指引我找到解决方案吗?

英文:

Im trying to get the list of installed AVD's in my machine by executing:

try {

    String exeCmd = sdkPath + &quot;/tools/bin/avdmanager list avd&quot;;

    Process process = Runtime.getRuntime().exec(exeCmd);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

} catch (IOException e) {
    e.printStackTrace();
}

After running this code I read the input and see if I have emulators to start.

This piece of code runs fine when executing the program on a Mac but throws an IOException when I run it on Windows, like so:

java.io.IOException: Cannot run program &quot;C:\Users\Myname\AppData\Local\Android\Sdk/tools/bin/avdmanager&quot;: CreateProcess error=2, The system cannot find the file specified (...)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessImpl.create(Native Method)
	at java.lang.ProcessImpl.&lt;init&gt;(ProcessImpl.java:444)
	at java.lang.ProcessImpl.start(ProcessImpl.java:139)
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
	... 7 more

Can someone help me resolve this problem or point me to a solution?

答案1

得分: 0

Welcome Fernando.

The code is using both Windows and Unix directory styles, hence you end up with a Windows path like so: "C:\Users\Myname\AppData\Local\Android\Sdk/tools/bin/avdmanager"

You see both the \ and the /. Since your Mac is Unix-based, sdkPath will resolve to a path with / separators.

To make your code portable (for Unix-based OS or not), use File.separator instead of /. That way, the JVM will produce the correct separator based on the underlying OS.

try {
    String exeCmd = sdkPath + File.separator + "tools" + File.separator + "bin" + File.separator + "avdmanager list avd";
    Process process = Runtime.getRuntime().exec(exeCmd);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
} catch (IOException e) {
    e.printStackTrace();
}

You can improve this by making it prettier using a different API to generate the path, but at least you get the gist of it.

英文:

Welcome Fernando.

The code is using both Windows and Unix directory style, hence you end up with a Windows path like so: &quot;C:\Users\Myname\AppData\Local\Android\Sdk/tools/bin/avdmanager&quot;

You see both the \ and the /. Since your Mac is unix based, sdkPath will resolve to a path with / separators.

To make your code portable (for Unix based OS or not), use File.separator instead of /. That way, the JVM will produce the correct separator based on the underlying OS.

try {

    String exeCmd = sdkPath + File.separator + &quot;tools&quot; + File.separator &quot;bin&quot; + File.separator + &quot;avdmanager list avd&quot;;

    Process process = Runtime.getRuntime().exec(exeCmd);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

} catch (IOException e) {
        e.printStackTrace();
}

You can improve this by making it prettier using a different API to generate the path, but at least you get the gist of it.

huangapple
  • 本文由 发表于 2020年8月11日 23:23:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63361349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定