A
A
Alexander Vasyuchenko2014-10-02 11:59:21
Android
Alexander Vasyuchenko, 2014-10-02 11:59:21

Runtime.getRuntime().exec: why doesn't "cp" command work everywhere?

Good afternoon!
There is this code:

File dir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS );
        File src = new File(dir, "test.dat");
        File dst = new File(dir, "test_2.dat");

        if ( src.exists() )
        {
            try
            {
                Process process = Runtime.getRuntime().exec( new String[] {"cp", "-f", src.getAbsolutePath(), dst.getAbsolutePath()} );
                process.waitFor();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

It works (copies file) on Samsung Galaxy S3 but fails on Amazon Kindle Fire tablet, throws an exception:
java.io.IOException: Error running exec(). Command: [cp, -f, /mnt/sdcard/Download/test.dat, /mnt/sdcard/Download/test_2.dat] Working Directory: null Environment: null
Why is this happening? And how to make this copy command run on all devices?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Vasyuchenko, 2014-10-02
@alexv1981

Understood. It turns out that there was no "cp" command on that tablet:
/system/bin/sh: cp: not found
Replaced with the "dd" command:
Runtime.getRuntime().exec( new String[] {"dd", "if=" +src.getAbsolutePath(), "of="+dst.getAbsolutePath()} )

V
Vladimir, 2014-10-16
@arcman

it's better to do "busybox cp ..."
for embedded systems, the entire toolchain is implemented through a single executable file - busybox.
it implements all the required functionality.
all other utilities are obtained by creating a hardlink to it (if busybox is called cp, then it will behave like cp).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question