简体   繁体   中英

How to set executable permissions on a file in Android to execute using “Runtime.getRuntime().exec(..)”

I'm currently trying to natively call on an executable on the shell from my Android Project which is in my application home directory. I can see via the command line when using ADB that my file is not getting executable file permissions, however. I've tried the following:

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

'executable' remains false.

I've also tried the following:

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

When I try to issue a command on the process, I get the following IOException:

java.io.IOException: Permission denied java.io.IOException: Error running exec(). Command: [/storage/emulated/0/myApp/iperf, -c, iperf.eltel.net] Working Directory: null Environment: null

The command I'm trying to issue is:

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

Again, at this stage, I can see that the file permissions on the process I wish to use is simply r/w and not executable. Any help appreciated! By the way, I'm trying to use the 'iPerf' C library and there is a compiled armeabi module as well as the original sources/JNI code. There are no compilation issues and this looks like a file permissions issue more than anything.

Files located on SD card aren't executable. Move the file to internal storage (eg to /data/local or, if you get permission denied, to data/local/tmp).

That is how I did it :

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

我认为Android的chmod无法理解u+x语法,请尝试使用744这样的数字符号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM