java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31874590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
提问by Marek
I want to add apache cli to my application, but I have problem. These errors show when I try to run it:
我想将 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
Here is my code:
这是我的代码:
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();
}
I also added in pom.xml this:
我还在 pom.xml 中添加了这个:
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
But it doesn't help :/ Also I added manually firstly commons-cli-1.3.1.jar and later commons-cli-1.2.jar but both doesn't help.
但它没有帮助:/我也首先手动添加了 commons-cli-1.3.1.jar 和后来的 commons-cli-1.2.jar 但两者都没有帮助。
@edit
@编辑
Ps. I'm running it as "java -jar filename.jar".
附言。我将它作为“java -jar filename.jar”运行。
回答by darijan
Try listing in the classpathall the jars that you are using:
尝试在类路径中列出您正在使用的所有 jar:
java -classpath lib/*.jar:other/location/lib/*jar:. my.package.Program
You must tell java which libraries to use to run the code.
您必须告诉 java 使用哪些库来运行代码。
回答by Garry
If you are using Maven then you can use AppAssembler plugin. It will packages your jar in a directory structure that contains your
如果您使用的是 Maven,那么您可以使用 AppAssembler 插件。它会将您的 jar 打包到包含您的目录结构中
- dependent jars
- the jar you created and
windows/linux scripts to execute it
- 依赖 jars
- 你创建的罐子和
windows/linux 脚本来执行它
回答by Amit Bhati
With few minute changes I am able to execute this code:-
只需几分钟的更改,我就可以执行此代码:-
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
In my pom.xml :-
在我的 pom.xml 中:-
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
commons-cli-1.2.jar is not visible to your code.
commons-cli-1.2.jar 对您的代码不可见。
回答by Ammar Atef
You need to pack the jar along with the dependencies.
您需要将 jar 与依赖项一起打包。
Add this to the plugins
tag in your pom.xml
file:
将此添加到文件中的plugins
标记中pom.xml
:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Your main class here</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
Then build your project with this command:
mvn clean compile assembly:single
然后使用以下命令构建您的项目:
mvn clean compile assembly:single
回答by Teo J.
What solved the problem for me was (I use ant to build my project) listing the commons-cli
dependency at the top of my ivy.xml
, not sure why this would make any difference but it did.
对我来说解决问题的是(我使用 ant 来构建我的项目)commons-cli
在我的顶部列出依赖项ivy.xml
,不知道为什么这会有所作为,但确实如此。
It might not be relevant to you since you use maven but might be helpful to somebody who uses ant.
由于您使用 maven,因此它可能与您无关,但可能对使用 ant 的人有所帮助。