0

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

Axel
  • 13,204
  • 4
  • 44
  • 72

4 Answers4

5

On globbing

Command line argument is parsed by the shell; this is why * is usually expanded to a list of files, for example. This is called "globbing", and it happens outside of your program, before the JVM even starts.

See also

Related questions


On splitting strings

As for splitting strings into array of strings, your most basic option is String.split(String regex).

Here's a very simple example:

    String[] parts = "one two three".split(" ");
    for (String part : parts) {
        System.out.println("[" + part + "]");
    }

The above prints:

[one]
[two]
[three]

The String argument to split is a regular expression.

References


Scanner option

Another option you can use is java.util.Scanner. This is a much more improved version of the legacy StringTokenizer.

Related questions


Guava option

For a more powerful String splitting functionality, you can use e.g. Splitter from Guava.

Community
  • 1
  • 1
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • 1
    Yes, thank you. Forgot about the shell's command line parsing. The reason why I don't want to use String.split is that it will get difficult when trying to handle quotes and escaped quotes within/without quoted strings etc. – Axel Jul 04 '10 at 07:41
  • @Axel: I'm not aware of a library that does this for you, but I think regex is the traditional tool for the job. You can write regex that can handle escaped quotes within quotes etc. This is quite a common task that a well-tested pattern is probably available in some regex patterns repository. – polygenelubricants Jul 04 '10 at 07:50
  • @Axel: have a look at the `Strings` pattern here: http://www.regular-expressions.info/examplesprogrammer.html – polygenelubricants Jul 04 '10 at 08:09
4

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();
Matthew Flaschen
  • 255,933
  • 45
  • 489
  • 528
  • I must have completely misunderstood the question (but +1 anyway) but what problem is being solved by using this technique? Can you elaborate? – polygenelubricants Jul 04 '10 at 08:03
  • @poly, the simplest way to parse a command line the exact same way as the shell is to use the shell. So if he has a command line meant for Program, this is the easiest way to pass it. – Matthew Flaschen Jul 04 '10 at 08:51
  • got it. If there's no real `Program` to speak of, though (i.e. "I" need the `String[]`, not `Program`) it would be interesting to use this technique to have `Program` pass its `String[]` from `main` back to "me" somehow. I'm not sure how to to do the interprocess communication, though. Probably RMI. – polygenelubricants Jul 04 '10 at 09:08
1

I just found another way by apache commons exec to fix this.

public static String[] splitArgsStr(String argsStr) {
    org.apache.commons.exec.CommandLine execCommandLine = new org.apache.commons.exec.CommandLine("sh");
    execCommandLine.addArguments(argsStr, false);
    return execCommandLine.getArguments();
}

And the latest maven dependency is:

<dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.3</version>
    </dependency>
烬哥哥
  • 291
  • 3
  • 4
0

That sort of parsing is usually done by the shell. So the function you're looking for would be part of a completely separate program, which is probably written in C. By the time the Java VM even gets started, the arguments have already been parsed.

David Z
  • 116,302
  • 26
  • 230
  • 268