Java 在运行时exec上执行多个执行
我正在尝试在Android中执行以下简单的unix ls:Java 在运行时exec上执行多个执行,java,android,runtime.exec,Java,Android,Runtime.exec,我正在尝试在Android中执行以下简单的unix ls: cd /data 然后 它应该返回/data文件夹的所有内容 我已将此编码为: try { String line; Process p = Runtime.getRuntime().exec(new String[] { "ls /data"}); BufferedReader in = new BufferedReader( new InputStreamRe
cd /data
然后
它应该返回/data文件夹的所有内容
我已将此编码为:
try {
String line;
Process p = Runtime.getRuntime().exec(new String[] { "ls /data"});
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
while ((line = in.readLine()) != null) {
Log.d("debugging", line);
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
我现在面临的问题是,我不能一次只执行一个命令。例如,如果我写ls/data
,它将不返回任何内容。他似乎不喜欢空间
如果我只写一个像“ls”这样的单词,返回根目录列表,一切都会正常工作:
03-19 22:51:59.241: D/debugging(16274): acct
03-19 22:51:59.241: D/debugging(16274): cache
03-19 22:51:59.241: D/debugging(16274): config
03-19 22:51:59.241: D/debugging(16274): crashtag
03-19 22:51:59.241: D/debugging(16274): d
03-19 22:51:59.241: D/debugging(16274): data
03-19 22:51:59.241: D/debugging(16274): default.prop
03-19 22:51:59.241: D/debugging(16274): dev
03-19 22:51:59.241: D/debugging(16274): etc
03-19 22:51:59.241: D/debugging(16274): fstab
03-19 22:51:59.241: D/debugging(16274): init
03-19 22:51:59.241: D/debugging(16274): init.clrdex.sh
03-19 22:51:59.241: D/debugging(16274): init.goldfish.rc
03-19 22:51:59.241: D/debugging(16274): init.hostapd.sh
03-19 22:51:59.241: D/debugging(16274): init.rc
03-19 22:51:59.241: D/debugging(16274): init.semc.rc
03-19 22:51:59.241: D/debugging(16274): init.usbmode.sh
03-19 22:51:59.241: D/debugging(16274): logo.rle
03-19 22:51:59.241: D/debugging(16274): mnt
03-19 22:51:59.241: D/debugging(16274): mr.log
03-19 22:51:59.241: D/debugging(16274): proc
03-19 22:51:59.241: D/debugging(16274): root
03-19 22:51:59.241: D/debugging(16274): sbin
03-19 22:51:59.241: D/debugging(16274): sdcard
03-19 22:51:59.241: D/debugging(16274): sys
03-19 22:51:59.241: D/debugging(16274): system
03-19 22:51:59.241: D/debugging(16274): ueventd.goldfish.rc
03-19 22:51:59.241: D/debugging(16274): ueventd.rc
03-19 22:51:59.241: D/debugging(16274): ueventd.semc.rc
03-19 22:51:59.241: D/debugging(16274): vendor
正如有人提到的,我尝试用多个命令填充该数组,但它什么也不返回。空白
{"ls","ls"} //this should return twice ls result.
有什么想法可以在Android运行时“连接”命令吗?我想你的问题在于
Process p = Runtime.getRuntime().exec(new String[] { "ls /data"});
您应该使用exec(字符串命令)或将“ls/data”分成两个字符串
Runtime.getRuntime().exec(new String[] { "ls", "/data"});
我认为您需要root访问权限才能执行
ls/data
命令,因此您应该首先获取su shell,然后执行命令,例如:
// run command with su rights and return output of that command(inside su
// shell)
// command = "ls /data"
public static void suOutputExecute(String command) {
try {
int BUFF_LEN = 1024;
Process p = Runtime.getRuntime().exec(new String[] { "su", "-c", "system/bin/sh" });
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
// from here all commands are executed with su permissions
stdin.writeBytes(command + "\n"); // \n executes the command
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
// while((read=stdout.read(buffer))>0) won't work here
while (true) {
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if (read < BUFF_LEN) {
// we have read everything
break;
}
}
stdout.close();
Log.e("ROOT", out);
p.waitFor();
} catch (Exception e) {
Log.e("ROOT", "Error", e);
}
}
如果您已经为java运行时研究过这个exec命令,请创建一个具有要“cd”到的路径的文件对象,然后将其作为exec方法的第三个参数输入
public Process exec(String command,
String[] envp,
File dir)
throws IOException
在具有指定环境和工作目录的单独进程中执行指定字符串命令
这是一种方便的方法。表单exec(command,envp,dir)的调用与调用exec(cmdarray,envp,dir)的行为完全相同,其中cmdarray是命令中所有令牌的数组
更准确地说,命令字符串使用由call new StringTokenizer(命令)创建的StringTokenizer分解为标记,而无需进一步修改字符类别。然后,标记器生成的标记按相同顺序放置在新的字符串数组cmdarray中
Parameters:
command - a specified system command.
envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
Returns:
A new Process object for managing the subprocess
Throws:
SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess
IOException - If an I/O error occurs
NullPointerException - If command is null, or one of the elements of envp is null
IllegalArgumentException - If command is empty
你真的需要System.exec吗?您是否考虑过使用文件()中的
listFiles()
方法?我会使用一个而不是Runtime.getRuntime.exec()
。我会这样做,因为这不是我最后要做的。我想调用一个系统程序,但如果我不能“同时”执行两个不同的命令,我将无法运行这些命令programs@SergiCastellsaguéMillán可能是您可以使用所需的命令生成.sh
脚本文件,并使用java运行该文件。我认为它与windows中的.bat
文件的概念相同<代码>Runtime.getRuntime().exec(新字符串[]{“ls”,“/data”})代码>显然没有返回任何数据。它是否引发异常?没有catch(Exception e){Log.d(“调试”,“异常”);e.printStackTrace();}
未打印任何内容。如果输入明显虚假的命令而不是“ls”,是否会出现任何错误?如果出于任何原因不能执行命令,它应该抛出IOException。不,我也这么认为。如果我输入“dsadasofpasnfas”,它也不会返回任何内容。可能是因为BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream())代码>只听标准输出而不听标准输出?我不确定…使用这段代码,我可以“读取”命令生成的输出吗?是的,至少对于第一个示例,我不完全确定第二个示例。
public Process exec(String command,
String[] envp,
File dir)
throws IOException
Parameters:
command - a specified system command.
envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.
Returns:
A new Process object for managing the subprocess
Throws:
SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess
IOException - If an I/O error occurs
NullPointerException - If command is null, or one of the elements of envp is null
IllegalArgumentException - If command is empty