检查这些: http://commons.apache.org/cli/ http://www.martiansoftware.com/jsap/ 或者自己动手: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html 例如, 这就是您使用 commons-cli 解析 2 个字符串参数的方式: import org.apache.commons.cli.*; public class Main { public static void main(String[] args) throws Exception { Options options = new Options(); Option input = new Option("i", "input", true, "input file path"); input.setRequired(true); options.addOption(input); Option output = new Option("o", "output", true, "output file"); output.setRequired(true); options.addOption(output); CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd = null;//not a good practice, it serves it purpose try { cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp("utility-name", options); System.exit(1); } String inputFilePath = cmd.getOptionValue("input"); String outputFilePath = cmd.getOptionValue("output"); System.out.println(inputFilePath); System.out.println(outputFilePath); } } 从命令行使用: $> java -jar target/my-utility.jar -i asd Missing required option: o usage: utility-name -i,--input <arg> input file path -o,--output <arg> output file 原文由 Vinko Vrsalovic 发布,翻译遵循 CC BY-SA 4.0 许可协议
检查这些:
或者自己动手:
例如, 这就是您使用
commons-cli
解析 2 个字符串参数的方式:从命令行使用: