简体   繁体   中英

Options in command line of Java jar file

I tried to search around but see no appropriate document showing how to do so I post this.

I want to call a function in my jar file to process a string given in command line such as:

java -jar myFile.jar -s "This is the string I want to process"

The used code is as follows:

Option inputStringOpt = new Option("s", "Input string");
options.addOption(inputStringOpt);
try {
     CommandLine commandLine = commandLineParser.parse(options, args);
     String inputString = commandLine.getOptionValue("s");
     //Call the required function
     someFunction(inputString);
}
catch (ParseException e) {
    System.err.println("Error: " + e.getMessage());
}

However, the inputString is always null. How can I read the desired string?

For more information, it throws out a NullPointerException with StringReader.

Thanks.

您的第一行应该是:

Option inputStringOpt = new Option("s", true, "Input string");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM