简体   繁体   English

Runtime.exec无法正常工作

[英]Runtime.exec not working

I'm trying to run the following code to swap filenames. 我正在尝试运行以下代码来交换文件名。 I'm using Runtime.exec . 我正在使用Runtime.exec The code throws IOException . 代码抛出IOException Anyway to fix this? 有任何解决这个问题的方法吗?

try {
Runtime.getRuntime().exec("file1=" + folderpath + " && file2=/mnt/sdcard/fsimages && temp=\"$(/system/xbin/mktemp -dp /mnt/sdcard)\" && /system/xbin/mv \"$file1\" $temp && /system/xbin/mv \"$file2\" \"$file1\" && /system/xbin/mv $temp/\"$file1\" \"$file2\"");
} catch (IOException e) {
    e.printStackTrace();
    return;
}

And the error: 而错误:

02-28 07:48:02.936: W/System.err(14399): java.io.IOException: Error running exec(). 02-28 07:48:02.936:W / System.err(14399):java.io.IOException:运行exec()时出错。 Command: [file1=/mnt/sdcard/fsimages_3, &&, file2=/mnt/sdcard/fsimages, &&, temp="$(/system/xbin/mktemp, -dp, /mnt/sdcard)", &&, /system/xbin/mv, "$file1", $temp, &&, /system/xbin/mv, "$file2", "$file1", &&, /system/xbin/mv, $temp/"$file1", "$file2"] Working Directory: null Environment: null 命令:[file1 = / mnt / sdcard / fsimages_3,&&,file2 = / mnt / sdcard / fsimages,&&,temp =“$(/ system / xbin / mktemp,-dp,/ mnt / sdcard)”,&&,/ system / xbin / mv,“$ file1”,$ temp,&&,/ system / xbin / mv,“$ file2”,“$ file1”,&&,/ system / xbin / mv,$ temp /“$ file1”, “$ file2”]工作目录:null环境:null

It looks like Runtime.exec is inserting a coma before and after every && . 看起来Runtime.exec在每个&&之前和之后插入一个昏迷 Seems like the issue is in the way which Runtime.exec interprets && . 似乎问题在于Runtime.exec解释&&的方式 Why is this happening? 为什么会这样? How can I prevent this? 我怎么能阻止这个?

If you use the Runtime.exec(String) overload, the string is treated as a command and its arguments, and is crudely split into substrings at white-space boundaries. 如果使用Runtime.exec(String)重载,则将字符串视为命令及其参数,并粗略地拆分为白色空间边界处的子字符串。 This splitting is standard behaviour for that overload. 这种分裂是该过载的标准行为。 (Refer to the javadoc.) (请参阅javadoc。)

Runtime.exec(...) expects a native command and its arguments. Runtime.exec(...)需要本机命令及其参数。 You've provided a line of shell input. 你提供了一系列shell输入。 The exec methods don't understand shell input and don't know how to execute it properly. exec方法不了解shell输入,也不知道如何正确执行它。 And the crude splitting (see above) messes up everything. 原油分裂(见上文)会弄乱一切。

If you need to do that, then use the following: 如果您需要这样做,请使用以下内容:

String yourShellInput = "echo hi && echo ho";  // or whatever ... 
String[] commandAndArgs = new String[]{ "/bin/sh", "-c", yourShellInput };
Runtime.getRuntime().exec(commandAndArgs);

This is equivalent to running: 这相当于运行:

$ /bin/sh -c "echo hi && echo ho".

If sh is not installed as /bin/sh use the path where it is installed instead. 如果未安装sh作为/bin/sh使用安装它的路径。

First of all the problem is not what you think "It looks like Runtime.exec is inserting a comma before and after every &&" actually the error statement is reporting your command that you gave in Runtime.exec() as String array (String[]). 首先问题不是你想的“看起来Runtime.exec在每个&&之前和之后插入一个逗号”实际上错误语句报告你在Runtime.exec()中给出的命令为String数组(String [ ])。 This is a standard behaviour of Java Runtime.exec() method. 这是Java Runtime.exec()方法的标准行为。

02-28 07:48:02.936: 
W/System.err(14399): java.io.IOException: 
Error runningexec(). Command: 
[file1=/mnt/sdcard/fsimages_3, &&, file2=/mnt/sdcard/fsimages, &&, temp="$(/system/xbin/mktemp, -dp, /mnt/sdcard)", &&, /system/xbin/mv, "$file1", $temp, &&, /system/xbin/mv, "$file2", "$file1", &&, /system/xbin/mv, $temp/"$file1", "$file2"] 
Working Directory: null Environment: null

You can see in the error how Java interprets your command which you hardcoded, the String array you are getting back shows you that "file1=/mnt/sdcard/fsimages_3" is treated as opening command to execute and the rest " &&" , "file2=/mnt/sdcard/fsimages" etc. is treated as arguments. 您可以在错误中看到Java如何解释您硬编码的命令,您回来的String数组显示“file1 = / mnt / sdcard / fsimages_3”被视为要执行的打开命令,其余为“&&”“ file2 = / mnt / sdcard / fsimages“等被视为参数。

What I think you should do is first try to split your command in an array like structure. 我认为你应该做的是首先尝试将命令拆分为类似结构的数组。 as an example below 以下是一个例子

Process process;
String[] commandAndArgs = new String[]{ "/bin/sh", "mv", "/mnt/sdcard/fsimages_3","/mnt/sdcard/fsimages" };
process = Runtime.getRuntime().exec(commandAndArgs);

Next, your error shows that your working directory is null and your Environment is also null. 接下来,您的错误显示您的工作目录为null,您的Environment也为null。 So you need to set both of them for your process. 所以你需要为你的过程设置它们。 From Java docs you can see it states this exec() overloaded method 从Java文档中,您可以看到它声明了这个exec()重载方法

exec(String[] cmdarray, String[] envp, File dir)

Executes the specified command and arguments in a separate process with the specified environment and working directory. 在具有指定环境和工作目录的单独进程中执行指定的命令和参数。

In case nothing stated works, I request you to kindly write a shell script file for your command and then set it executable using 如果没有任何说明可行,我请求您为您的命令编写一个shell脚本文件,然后使用它设置它可执行文件

chmod 755 or chmod +x script.sh;

then try to run that. 然后尝试运行它。 I hope it works as in my case the script file approach worked. 我希望它在我的情况下工作,脚本文件方法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM