Split a string containing command-line parameters into a String[] in Java

后端 未结 6 906
孤街浪徒
孤街浪徒 2020-12-05 10:14

Similar to this thread for C#, I need to split a string containing the command line arguments to my program so I can allow users to easily run multiple commands. For exampl

6条回答
  •  醉梦人生
    2020-12-05 10:36

    Here is a pretty easy alternative for splitting a text line from a file into an argument vector so that you can feed it into your options parser:

    This is the solution:

    public static void main(String[] args) {
        String myArgs[] = Commandline.translateCommandline("-a hello -b world -c \"Hello world\"");
        for (String arg:myArgs)
            System.out.println(arg);
    }
    

    The magic class Commandline is part of ant. So you either have to put ant on the classpath or just take the Commandline class as the used method is static.

提交回复
热议问题