Question

I'm trying to copy a file from /path/to the/file.ext (yes, it has spaces, I suspect this is at least part of the trouble) to /data/data/com.my_pkg.app/file.ext.

In my (root) app, I do:

String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";
try {
    Process process;
    process = new ProcessBuilder(cmd).start();
    try {
        process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

But this raises an IOException: no such file or directory.

Checking my sanity, I opened a root file explorer app, navigated to the path above, copied it, and pasted it in my app's directory, and everything was fine.

Why is cp not finding it? - the file explorer app must be doing the same (well, with modification that makes it work!) thing underneath all that GUI.

Edit: the full error:

W/System.err( 2441): java.io.IOException: Error running exec(). 
     Command: [su -c "cp /path/to\ the/file.ext /data/data/com.my_pkg.app/file.ext"] 
     Working Directory: null 
     Environment: [ANDROID_ROOT=/system, 
          EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, 
          LOOP_MOUNTPOINT=/mnt/obb, 
          EMULATED_STORAGE_TARGET=/storage/emulated, 
          ANDROID_BOOTLOGO=1, LD_LIBRARY_PATH=/vendor/lib:/system/lib,             
          EXTERNAL_STORAGE=/storage/emulated/legacy, 
          ANDROID_SOCKET_zygote=10, 
          ANDROID_DATA=/data, 
          PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, 
          ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, 
          BOOTCLASSPATH=/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/webviewchromium.jar, 
          ANDROID_PROPERTY_WORKSPACE=9,0, 
          ANDROID_STORAGE=/storage]
Was it helpful?

Solution

I'm posting this as an answer, because it resolved my question as I asked it, and so should solve this problem for any one happening across this by search. For me, however, it just led to another question.

By changing the string:

String cmd = "su -c \"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\"";

to an array (that the rest of the code as-is concatenates/builds):

String[] cmd = {"su", "-c", "\"cp /path/to\\ the/file.ext /data/data/com.my_pkg.app/file.ext\""};

All the errors were resolved. I can't tell you why, but at least they are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top