gpt4 book ai didi

java - 在 Android 中运行 native 可执行文件出现错误

转载 作者:行者123 更新时间:2023-12-02 08:29:30 25 4
gpt4 key购买 nike

我试图使用下面的代码来运行 native ,但我在 Class execClass = Class.forName("android.os.Exec") 中得到了 android.os.exec 的 classnotfoundException...你有什么想法吗? p>

    try {
// android.os.Exec is not included in android.jar so we need to use reflection.
Class<?> execClass = Class.forName("android.os.Exec");
Method createSubprocess = execClass.getMethod("createSubprocess",
String.class, String.class, String.class, int[].class);
Method waitFor = execClass.getMethod("waitFor", int.class);

// Executes the command.
// NOTE: createSubprocess() is asynchronous.
int[] pid = new int[1];
FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(
null, "/system/bin/ls", "/sdcard", null, pid);

// Reads stdout.
// NOTE: You can write to stdin of the command using new FileOutputStream(fd).
FileInputStream in = new FileInputStream(fd);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String output = "";
try {
String line;
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
} catch (IOException e) {
// It seems IOException is thrown when it reaches EOF.
}

// Waits for the command to finish.
waitFor.invoke(null, pid[0]);

return output;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage());
} catch (SecurityException e) {
throw new RuntimeException(e.getMessage());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e.getMessage());
} catch (IllegalArgumentException e) {
throw new RuntimeException(e.getMessage());
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getMessage());
}

链接:http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App

最佳答案

android.os.Exec 不是公共(public) API 的一部分,不应使用。事实上,自 1.6 版本以来,它就不再是产品的一部分,这应该是进一步的激励。 :-)

您应该使用标准的 Java 语言工具,例如 Runtime.exec() 或 ProcessBuilder.start()。

关于java - 在 Android 中运行 native 可执行文件出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3783828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com