Java 在Android错误中运行本机可执行文件

Java 在Android错误中运行本机可执行文件,java,android,Java,Android,我试图使用下面的代码运行一个本机程序,我在类execClass=Class.forName(“android.os.exec”)中得到了一个android.os.exec的classnotfoundexception…你知道吗 try { // android.os.Exec is not included in android.jar so we need to use reflection. Class<?> execClass = Class.forNa

我试图使用下面的代码运行一个本机程序,我在类execClass=Class.forName(“android.os.exec”)中得到了一个android.os.exec的classnotfoundexception…你知道吗

    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());
}
试试看{
//android.jar中不包含android.os.Exec,因此我们需要使用反射。
Class execClass=Class.forName(“android.os.Exec”);
方法createSubprocess=execClass.getMethod(“createSubprocess”,
String.class、String.class、String.class、int[].class);
方法waitFor=execClass.getMethod(“waitFor”,int.class);
//执行命令。
//注意:createSubprocess()是异步的。
int[]pid=新的int[1];
FileDescriptor fd=(FileDescriptor)createSubprocess.invoke(
空,“/system/bin/ls”、“/sdcard”、空,pid);
//读到stdout。
//注意:您可以使用新的FileOutputStream(fd)写入命令的stdin。
FileInputStream in=新的FileInputStream(fd);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(in));
字符串输出=”;
试一试{
弦线;
而((line=reader.readLine())!=null){
输出+=行+“\n”;
}
}捕获(IOE异常){
//似乎IOException在到达EOF时被抛出。
}
//等待命令完成。
invoke(null,pid[0]);
返回输出;
}catch(classnotfounde异常){
抛出新的RuntimeException(例如getMessage());
}捕获(安全异常e){
抛出新的RuntimeException(例如getMessage());
}捕获(无此方法例外){
抛出新的RuntimeException(例如getMessage());
}捕获(IllegalArgumentException e){
抛出新的RuntimeException(例如getMessage());
}捕获(非法访问例外e){
抛出新的RuntimeException(例如getMessage());
}捕获(调用TargetException e){
抛出新的RuntimeException(例如getMessage());
}

链接:

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


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

了解您使用的Android版本可能会有所帮助。