@Adi Tiwari, I've found the cause.
Runtime.getRuntime.exec() doesn't execute a shell command directly, it executes an executable with arguments.
"echo" is a builtin shell command. It is actually a part of the argument of the executable sh with the option -c.
Commands like ls are actual executables.
You can use type echo and type ls command in adb shell to see the difference.
So final code is:
String[] cmdline = { "sh", "-c", "echo $BOOTCLASSPATH" };
Runtime.getRuntime().exec(cmdline);
Answer from QY Lin on Stack OverflowFinally after weeks of searching:
Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
reboot works fine in android. you are probably not doing runtime.exec() properly. you need to handle the
public static void rebootSU() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;
StringBuilder sbstdOut = new StringBuilder();
StringBuilder sbstdErr = new StringBuilder();
String command="/system/bin/reboot";
try { // Run Script
proc = runtime.exec("su");
osw = new OutputStreamWriter(proc.getOutputStream());
osw.write(command);
osw.flush();
osw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
if (proc != null)
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
.getInputStream())));
sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
.getErrorStream())));
if (proc.exitValue() != 0) {
}
}
First, you should wrap calls to exec in a try-catch-clause to catch IOExceptions.
Second, use exec(java.lang.String[]) to execute a command with parameters. For example, similar to
Runtime.getRuntime().exec(new String[]{ "shell", "/system/bin/chmod", "0777", sdCard.getAbsolutePath() + "/" + nativeFile });
The sdcard in an Android system is usually disabled for execution. Therefore even if you correctly execute the chmod command it will fail.
You can test that easily. Start the shell via USB (adb shell) and execute the chmod command. It will fail with an error message like "Bad mode".
Therefore you have to copy the file to a different location where you have write access and then set the executable bit on that copy. You can try to copy the file for example to "/data/local/tmp/" but I am not sure if that path is still usable.
You can execute ls with provided path for which folders should be listed
Runtime.getRuntime().exec(new String[] { "ls", "\\tmp"});
The current working directory is associated with the current process. When you exec("cd tmp") you are creating a process, changing its directory to "tmp", and then the process exits. The parent process' working directory doesn't change.
See this for a more general discussion of changing the current working directory in Java.
Redirection (>) is not the OS feature. This is a feature of shell. To make it working from java you have to run something like the following:
/bin/sh yourcommand > yourfile
i.e. in your case:
/bin/sh cat /sdcard/file1.mpg /sdcard/file2.mpg > /sdcard/out.mpg
BUT could you please explain me why are you doeing this? Do you understand that this command is exact equivalent of cp /sdcard/file1.mpg /sdcard/file2.mpg /sdcard/out.mpg that can be coded in pure java without running any command line? Unless you have special reasons go on it! Write pure java code when it is possible. It is easier to debug, support and maintain.
There's absolutely no reason to use 'cat' to do this. It's not a supported or encouraged mechanism on Android, and there's no reason to launch a new executable to do what you can easily do in java code, by reading in one file and writing it out to the other.
For the record, you are trying to do a shell redirection, and that will not work since you are not executing a shell.