Maximum length of string argument in java processbuilder

拥有回忆 提交于 2019-12-24 18:42:31

问题


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 lpCommandLinestates: "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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!