2014-04-01 122 views
0

我目前正嘗試本機地從我的Android項目中調用一個可執行文件我的應用程序主目錄。但是,使用ADB時,我可以通過命令行看到我的文件沒有獲得可執行文件權限。我試過以下內容:如何在Android中使用「Runtime.getRuntime()。exec(..)」執行文件的可執行權限設置

File file = new File(myFileLocation); 
if(file.exists()){ 
boolean executable = file.setExecutable(true); 
} 

'可執行'仍然是錯誤的。

我也試過如下:

Process processChmod = Runtime.getRuntime().exec("/system/bin/chmod u+x " + myExecutableLocation); 
processChmod.waitFor(); 

當我嘗試的過程中發出的命令,我得到以下IOException異常:

產生java.io.IOException:權限被拒絕java.io.IOException:運行exec()的錯誤 。命令:[/存儲/模擬/ 0 /對myApp/iperf的,-c, iperf.eltel.net]工作目錄:空環境:空

我想問題的命令是:

Runtime.getRuntime().exec("/storage/emulated/0/myApp/iperf -c iperf.eltel.net") 

再次,在這個階段,我可以看到,我希望使用的進程的文件權限只是簡單的r/w而不可執行。任何幫助感謝!順便說一下,我試圖使用'iPerf'C庫,並且有一個編譯好的armeabi模塊以及原始源代碼/ JNI代碼。沒有編譯問題,這看起來像文件權限問題比什麼都重要。

回答

0

我認爲Android的chmod不理解u+x語法,請嘗試使用數字符號,如744

0

位於SD卡上的文件不可執行。將文件移動到內部存儲器(例如,移到/ data/local,或者如果您獲得權限被拒絕,移到data/local/tmp)。

這就是我做的:

String filePath = getFilesDir().getAbsolutePath() + File.separator + "myFileName"; 
File myFile = new File(filePath); 
if (!myFile.canExecute()) { 
    Log.d("File is not executable, trying to make it executable ..."); 
    if (myFile .setExecutable(true)) { 
     Log.d("File is executable"); 
    } else { 
     Log.d("Failed to make the File executable"); 
    } 
} else { 
    Log.d("File is executable"); 
} 
相關問題