Search code examples
javastringparsingcode-reuse

Split string with quotes, reusing method that parses arguments to main


in small program I'm writing, I have to parse a line of user input. Basically what needs to be done is to split the line into an array of strings in the same way as is done with the arguments to main(), ie I'm looking for something like this:

String[] splitArgs(String cmdLine);

I just wonder, if the main methods' arguments are parsed in this way prior to invoking main itself, wouldn't it be possible to call that one instead of writing your own? So, does anyone know where to find that method?

Thanks, Axel


Solution

  • No, that parsing is done by the shell or system library (on Windows), not by Java. You could use ProcessBuilder and sh -c (or cmd on Windows). Something like:

    new ProcessBuilder("sh", "-c", "java Program " + cmdLine).start();