I have maven project and I'm trying to use commons cli library to handle input data:
import org.apache.commons.cli.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Cli {
private static final Logger log = Logger.getLogger(Cli.class.getName());
private String[] args = null;
private Options options = new Options();
public Cli(String[] args) {
this.args = args;
options.addOption("h", "help", false, "show help.");
options.addOption("i", "inputs", true, "Enter the input file name.");
options.addOption("o", "output", true, "Enter the output file name.");
}
public String[] parse() {
CommandLineParser parser = new BasicParser();
String[] returnArguments = new String[2];
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
if (cmd.hasOption("h"))
help();
if (cmd.hasOption("i")) {
log.log(Level.INFO, "The initial file will be " + cmd.getOptionValue("i"));
returnArguments[0] = cmd.getOptionValue("i");
} else {
log.log(Level.SEVERE, "Missing input file name option");
help();
}
if (cmd.hasOption("o")) {
log.log(Level.INFO, "The output file will be " + cmd.getOptionValue("o"));
returnArguments[1] = cmd.getOptionValue("o");
} else {
log.log(Level.SEVERE, "Missing output file name option");
help();
}
} catch (ParseException e) {
log.log(Level.SEVERE, "Failed to parse command line properties", e);
help();
}
return returnArguments;
}
private void help() {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("Main", options);
System.exit(0);
}
}
I set arguments in Intellij Idea: -i inputs.zip -o output.zip It works, but what command should I use in console?
I tried: java -jar NewMyZip-1.jar -i inputs.zip -o output.zip and got the exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cl
i/ParseException
at main.MyZip.main(MyZip.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseExcepti
on
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Does anybody know what is the problem and how to solve it ?