String cmd = "ll";
Process process = Runtime.getRuntime().exec(cmd);
// java.io.IOException: Error running exec(). Command: [ll]
ls:/system/bin/ls 真實二進位檔。ll:通常定義於 ~/.bashrc 或 /etc/profile 中的 alias ll='ls -l',**僅在互動式 Shell 有效**。Runtime.exec() **不啟動 Shell**,直接呼叫 execve,找不到 ll 執行檔 → 失敗。String cmd = "ls -l";(推薦,跨平台)。
String[] cmd = {"/system/bin/sh", "-c", "ll"};
Process process = Runtime.getRuntime().exec(cmd);
但 Android 預設 Shell 可能不載入使用者別名,仍可能失效。
| 需求 | 指令 | 說明 |
|---|---|---|
| 詳細列表 | ls -l | POSIX 標準,所有 Unix-like 通用 |
| 顯示隱藏檔 | ls -a | 標準參數 |
| 人類可讀大小 | ls -lh | GNU coreutils 擴充,BusyBox 可能不支援 -h |
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
Log.i("CMD", line);
}
}
結論:Android 無 ll 指令,請用 ls -l。不要假設桌面 Linux 的別名在 Android 存在。