java IOException: error=7, 参数列表太长,命令行很大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16912103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:22:34  来源:igfitidea点击:

IOException: error=7, Argument list too long with large command line

javaunixprocess

提问by VamsiKrishna

I have a requirement to call Unix command from Java. The code is as follows.

我需要从 Java 调用 Unix 命令。代码如下。

String strCmd = "iconv -f "+ strSrcEncoding+" -t "+ strTgtEncoding + " <<< "+"\""+InputMessage+"\"";

String commands[] = {"bash","-c",strCmd};
Process proc = Runtime.getRuntime().exec(commands);
String strData = null;

// Get the error Stream
BufferedReader brStdError = new BufferedReader(new
        InputStreamReader(proc.getErrorStream()));
StringBuilder sbError = new StringBuilder();


// read any errors from the attempted command
while ((strData = brStdError.readLine()) != null) {
    sbError.append(strData);
}

if(sbError.toString().isEmpty())
    return "success";
else
    return "failure"+sbError.toString();

When i pass a large data am getting an error

当我传递大数据时出现错误

"bash": java.io.IOException: error=7, Argument list too long

I tried using echo instead as below

我尝试使用 echo 代替如下

echo <<InputMessage>> | iconv -f utf8 -t Cp930

But was getting the same error

但是得到了同样的错误

Am i missing anything?

我错过了什么吗?

回答by Joni

There's a limit to much data you can pass to a program on the command line. If you have a lot of data you should pass it to iconv using its standard input stream, i.e. by writing it to proc.getOutputStream. Here's an example:

您可以在命令行上传递给程序的数据量是有限的。如果您有大量数据,则应使用其标准输入流将其传递给 iconv,即通过将其写入proc.getOutputStream. 下面是一个例子:

OutputStream os = proc.getOutputStream();
os.write(InputMessage.getBytes(strSrcEncoding));
os.close();

Unfortunately, for longer messages this will also fail because iconvwill fill the buffer of the pipe it is connected to, and wait for the pipe to be read. The solution is writing data to iconvfrom one thread and reading the output from another. Dealing with external processes is a hassle because of all these pit-falls. You can read more about it here: http://www.javaworld.com/jw-12-2000/jw-1229-traps.htmlApache commons exechelps you deal with some of them.

不幸的是,对于较长的消息,这也会失败,因为iconv它将填充它所连接的管道的缓冲区,并等待管道被读取。解决方案是iconv从一个线程写入数据并从另一个线程读取输出。由于所有这些陷阱,处理外部流程很麻烦。您可以在此处阅读更多相关信息:http: //www.javaworld.com/jw-12-2000/jw-1229-traps.html Apache commons exec可帮助您处理其中的一些问题。

On the other hand, why are you using iconv? You know Java has good library support for most character encodings, cp930included?

另一方面,你为什么使用 iconv?您知道 Java 对大多数字符编码都有很好的库支持,cp930包括在内?

回答by Kondal Kolipaka

This happens, when the size of the arguments exceeds the allowed size. And this depends on the platform.

当参数的大小超过允许的大小时,就会发生这种情况。这取决于平台。

To verify the maximum limit, run

要验证最大限制,请运行

$ getconf ARG_MAX

For mac osx environment, I can see 262144 is the limit.

对于 mac osx 环境,我可以看到 262144 是限制。

Limit for each platform is different, that can be found here: http://www.in-ulm.de/~mascheck/various/argmax/

每个平台的限制是不同的,可以在这里找到:http: //www.in-ulm.de/~mascheck/various/argmax/

To check the environment string:

要检查环境字符串:

$env

回答by jan.supol

I reached this issue when trying to set environment variables as follows:

我在尝试设置环境变量时遇到了这个问题,如下所示:

final ProcessBuilder pb = new ProcessBuilder(...);
pb.environment().putAll(env); //here it's ok
...
pb.start(); //here is where it throws the exception

The problem was with what the envmap contained, though the "bash":java.io.IOException: error=7, Argument list too longwas indicating otherwise.

问题出在env地图所包含的内容上,尽管"bash":java.io.IOException: error=7, Argument list too long另有说明。