我想在我的应用程序中添加apache cli,但是我有问题.当我尝试运行它时会显示这些错误:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("a", "abc", true, "First parameter");
try {
CommandLine commandLine = parser.parse(options, args);
System.out.println(commandLine.getOptionValue("a"));
} catch (ParseException e1) {
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我还在pom.xml中添加了这个:
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但它没有帮助:/我还首先手动添加了commons-cli-1.3.1.jar和后来的commons-cli-1.2.jar,但两者都无济于事.
@编辑
PS.我将它作为"java -jar filename.jar"运行.
经过几分钟的更改,我就能执行以下代码:
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("a", true, "First parameter");
args=new String[]{"-a abc"};
try {
CommandLine commandLine = parser.parse(options, args );
System.out.println(commandLine.getOptionValue("a"));
} catch (ParseException e1) {
e1.printStackTrace();
}
Output :- abc
Run Code Online (Sandbox Code Playgroud)
在我的pom.xml中:-
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
commons-cli-1.2.jar对您的代码不可见。
尝试在类路径中列出您正在使用的所有 jar:
java -classpath lib/*.jar:other/location/lib/*jar:. my.package.Program
Run Code Online (Sandbox Code Playgroud)
您必须告诉 java 使用哪些库来运行代码。
| 归档时间: |
|
| 查看次数: |
21767 次 |
| 最近记录: |