我有maven项目,我试图使用commons cli库来处理输入数据:
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);
}
}
我在Intellij Idea中设置了参数:-i inputs.zip -o output.zip 它可以工作,但我应该在控制台中使用什么命令?
我试过:java -jar NewMyZip-1.jar -i inputs.zip -o output.zip 得到了例外:
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
有谁知道问题是什么以及如何解决?
答案 0 :(得分:1)
你的NewMyZip-1.jar是否包含commons-cli?否则,请确保在类路径中包含commons-cli jar:
java -cp PATH_TO_JAR/commons-cli.jar -jar NewMyZip-1.jar -i inputs.zip -o output.zip