Runtime.exec() String length

Discussion in 'Java' started by Prime, Dec 16, 2005.

  1. Prime

    Prime Guest

    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
    Prime, Dec 16, 2005
    #1
    1. Advertising

  2. On 2005-12-16, Prime penned:
    > 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)

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Monique Y. Mudama, Dec 16, 2005
    #2
    1. Advertising

  3. Prime

    Alun Harford Guest

    "Prime" <> wrote in message
    news:...
    > 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
    Alun Harford, Dec 16, 2005
    #3
  4. On 16 Dec 2005 08:39:17 -0800, Prime wrote:
    > 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

    --
    [ 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
    Gordon Beaton, Dec 16, 2005
    #4
  5. Prime

    Prime Guest

    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
    Prime, Dec 19, 2005
    #5
  6. On 19 Dec 2005 05:46:34 -0800, Prime wrote:
    > 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

    --
    [ 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
    Gordon Beaton, Dec 19, 2005
    #6
  7. Prime

    Nigel Wade Guest

    Prime wrote:

    > 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.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Dec 19, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    3
    Views:
    14,091
    Karl Seguin
    Feb 17, 2005
  2. Replies:
    2
    Views:
    435
    Alan Moore
    Mar 31, 2005
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,104
    Gordon Beaton
    May 22, 2006
  4. Replies:
    4
    Views:
    24,060
    vivek_kothari
    Jan 12, 2010
  5. Replies:
    5
    Views:
    662
    John W. Kennedy
    Jan 11, 2007
Loading...

Share This Page