简体   繁体   English

Java ProcessBuilder中字符串参数的最大长度

[英]Maximum length of string argument in java processbuilder

I know there is a string limitation when we execute application from command line / terminal which depends on the OS. 我知道从命令行/终端执行应用程序时,存在字符串限制,具体取决于操作系统。
But if we use the Java ProcessBuilder to execute application, is there any maximum string length to be passed as argument in the java ProcessBuilder ? 但是,如果我们使用Java ProcessBuilder执行应用程序,那么在Java ProcessBuilder中是否有任何最大字符串长度作为参数传递?

   ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

Thanks 谢谢

If you look at ProcessBuilder source code the process creation is delegated to a OS specific java.lang.ProcessImpl . 如果查看ProcessBuilder源代码,则将流程创建委托给特定于OS的java.lang.ProcessImpl

For instance the Windows version of ProcessImpl calls CreateProcess in the Win API, passing the commandline string. 例如,Windows版本的ProcessImpl在Win API中调用命令行中的CreateProcess The documentation for parameter lpCommandLine states: "The maximum length of this string is 32,768 characters". 参数lpCommandLine的文档指出:“此字符串的最大长度为32,768个字符”。

So yes there will be OS specific length limitations, based on the OS function to create the process. 因此,是的,基于创建过程的OS功能,将存在特定于OS的长度限制。

Java doesnt restrict the length. Java不限制长度。 You can pass n parameters but it depends on the OS. 您可以传递n个参数,但这取决于操作系统。

Description from Java Doc ProcessBuilder Java Doc ProcessBuilder中的描述

Constructs a process builder with the specified operating system program and arguments. 使用指定的操作系统程序和参数构造一个流程生成器。 This is a convenience constructor that sets the process builder's command to a string list containing the same strings as the command array, in the same order. 这是一个方便的构造函数,用于将流程生成器的命令设置为包含与命令数组相同的字符串的字符串列表,并且顺序相同。 It is not checked whether command corresponds to a valid operating system command. 不检查命令是否对应于有效的操作系统命令。

Usually we don't pass more than 4 args in commandline rather keep everything in file and provide file name as single argument and use the config file. 通常,我们在命令行中传递的参数不超过4个,而是将所有内容保留在文件中,并提供文件名作为单个参数并使用配置文件。

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

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