开发者

Run native executable in Android error

I was trying to use this code below to run a native and i get a classnotfoundexception for android.os.exec in Class execClass = Class.forName("android.os.Exec")..any idea y?

    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: createSubpr开发者_开发问答ocess() 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());
}

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


android.os.Exec is not part of the public API, and should not be used. The fact that it hasn't been part of the product since 1.6 should be further incentive. :-)

You should use the standard Java-language facilities instead, like Runtime.exec() or ProcessBuilder.start().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜