Runtime.exec() String length

P

Prime

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
 
M

Monique Y. Mudama

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

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)
 
A

Alun Harford

Prime said:
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".

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
 
G

Gordon Beaton

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
 
P

Prime

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
 
G

Gordon Beaton

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
 
N

Nigel Wade

Prime said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top