Hi all,
Does anyone know if there is a maximum length the String variable can
be in a Runtime.exec( ) command, (as in like maximum characters)? I've
searched through the API documentation, haven't found anything.
Reason for asking is I don't want the command String to be cut off
prematurely. I'm creating a .tar file, building the command to
Runtime.exec() through a StringBuffer, and looping through a List of
filenames and don't know how big that list could be. i.e. "tar -cvf
archiveName file1 file2 file3 ....fileX".
Thank you!
Patrick
Monique Y. Mudama - 16 Dec 2005 17:01 GMT
> Hi all,
>
[quoted text clipped - 12 lines]
>
> Patrick
I wouldn't expect there to be a java limitation, as it uses Strings; I
would expect this to be a system-specific issue. It probably depends
on what OS you're using.
By the way, do you really need to do all that looping? Take a look at
Runtime.exec(String[] cmdarray)

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Alun Harford - 16 Dec 2005 17:42 GMT
> Hi all,
>
[quoted text clipped - 7 lines]
> filenames and don't know how big that list could be. i.e. "tar -cvf
> archiveName file1 file2 file3 ....fileX".
I believe it's OS dependent, and also JVM dependent.
I've not tested it, but I suspect that in Windows the maximum length will be
either:
32k - the maximum size string that the CreateProcess function will take
8k - maximum length string cmd.exe will take
2k - INTERNET_MAX_URL_LENGTH
In *nix, it's probably large.
Alun Harford
Gordon Beaton - 16 Dec 2005 18:13 GMT
> I'm creating a .tar file, building the command to Runtime.exec()
> through a StringBuffer, and looping through a List of filenames and
> don't know how big that list could be. i.e. "tar -cvf archiveName
> file1 file2 file3 ....fileX".
If you expect X to be large, consider telling tar to read the file
list from stdin (tar -T - -cf archive.tar), then printing the
filenames to process.getOutputStream() instead.
Also, I don't see the point of specifying -v (verbose) when you run
this as a subprocess to your Java app. You have to deal with all that
unnecessary output or the process will hang.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Prime - 19 Dec 2005 13:46 GMT
First off thank you for the replies!
Second, follow up question:
Any ideas on where to research this? Javadocs for String, Process and
Runtime haven't turned up anything for a maximum length of the String,
and so far neither have MAN pages. Also, a lot of the Java reference
books I have don't go into great detail on the subject, (and I know
why, .exec-ing not being platform independent and all that).
Thanks again!
Patrick
Gordon Beaton - 19 Dec 2005 14:04 GMT
> Any ideas on where to research this? Javadocs for String, Process
> and Runtime haven't turned up anything for a maximum length of the
> String, and so far neither have MAN pages. Also, a lot of the Java
> reference books I have don't go into great detail on the subject,
> (and I know why, .exec-ing not being platform independent and all
> that).
On Linux and Unix, Runtime.exec() is implemented as a call to execve()
or similar, so have a look at the manpage for that system call on the
relevant platform.
On Linux, execve() returns E2BIG when the argument list is too big.
After poking around the kernel sources it seems the limit is defined
as 32 pages or about 128K (assuming PAGE_SIZE 4096, which may not be
the case on all architectures and kernel versions).
Earlier I suggested that you can avoid this problem completely by
having tar read the file list from stdin (or a file).
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Nigel Wade - 19 Dec 2005 14:18 GMT
> Hi all,
>
[quoted text clipped - 11 lines]
>
> Patrick
As an alternative, can you use cpio? Cpio reads filenames from standard input
and creates an archive of those files, which can be a tar format archive.
You could create a cpio process, and send the filenames to the standard input of
that process.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555