2010-09-24 5 views
0

J'essayais d'utiliser ce code ci-dessous pour exécuter un natif et j'obtiens une exception classnotfound pour android.os.exec dans la classe execClass = Class.forName ("android.os.Exec") .. une idée y?Exécuter l'exécutable natif dans l'erreur Android

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()); 
} 

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

+0

Il peut être utile de connaître la version d'Android que vous utilisez. – MatrixFrog

+0

J'utilise Android 2.2 – Jony

Répondre

2

android.os.Exec ne fait pas partie de l'API publique, et ne doit pas être utilisé. Le fait qu'il n'ait pas fait partie du produit depuis la version 1.6 devrait être une incitation supplémentaire. :-)

Vous devriez utiliser les fonctions standard du langage Java à la place, comme Runtime.exec() ou ProcessBuilder.start().

Questions connexes