问题
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 ?
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Thanks
回答1:
If you look at ProcessBuilder source code the process creation is delegated to a OS specific java.lang.ProcessImpl
.
For instance the Windows version of ProcessImpl
calls CreateProcess in the Win API, passing the commandline string. The documentation for parameter lpCommandLine
states: "The maximum length of this string is 32,768 characters".
So yes there will be OS specific length limitations, based on the OS function to create the process.
回答2:
Java doesnt restrict the length. You can pass n parameters but it depends on the OS.
Description from 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.
来源:https://stackoverflow.com/questions/32202468/maximum-length-of-string-argument-in-java-processbuilder