Java.lang.NoClassDefFoundError: org / apache / commons / cli / ParseException

I want to add apache cli to my application, but I have a problem. These errors show when I try to run it:

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 the following to pom.xml:

 <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.2</version> </dependency> 

But this does not help: / I also added manually first commons-cli-1.3.1.jar and later commons-cli-1.2.jar, but both do not help.

@edit

Ps. I run it as "java -jar filename.jar".

+9
source share
4 answers

With a few minutes, I can 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: -

  <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.2</version> </dependency> 

commons-cli-1.2.jar does not appear in your code.

+4
source

Try specifying all used banks in the classpath :

 java -classpath lib/*.jar:other/location/lib/*jar:. my.package.Program 

You must tell java which libraries to use to run the code.

+3
source

If you are using Maven, you can use the AppAssembler plugin. It packs your jar into a directory structure containing your

0
source

You need to pack the jar along with the dependencies.

Add this to the plugins tag in your pom.xml file:

  <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

0
source

All Articles