英文:
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 + "/tools/bin/avdmanager list avd";
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 "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
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: "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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论