简体   繁体   中英

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

I want to add apache cli to my application, but I have 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 in pom.xml this:

<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.

@edit

Ps. I'm running it as "java -jar filename.jar".

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 :-

  <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.

Try listing in the classpath all the jars that you are using:

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

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

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

If you are using Maven then you can use AppAssembler plugin. It will packages your jar in a directory structure that contains your

  1. dependent jars
  2. the jar you created and
  3. windows/linux scripts to execute it

    appassembler-maven-plugin

What solved the problem for me (I use ant to build my project) was listing the commons-cli dependency at the top of my ivy.xml , not sure why this would make any difference but it did.

It might not be relevant to you since you use maven but might be helpful to somebody who uses ant.

I ran into a similar NoClassDefFoundError exception.

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:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 7 more

Oddly, one build was failing with the above error and a different one was working.

The differences I found were with the

  • Generated jar file, mainly in META-INF/MANIFEST.MF in the jar file.
    • It was missing Class-Path
    • The maven versions used by the build pipeline (AzureDevOps one) were different between the releases.
      • The failing one had 3.8.3 and
      • The one that works had 3.8.4 .

Not sure if the maven versions or the dependencies supported by the maven version were changing during the build time and we were caught off-guard.

在此处输入图片说明

To provide some background, our program distribution or package looks like below

target/program
├── program-1.0.0.jar
└── lib
    ├── activation-1.1.1.jar
...<truncated jars>
    └── xnio-nio-3.3.6.Final.jar

and you can run the program using a command like

cd target/program;
java -jar program-1.0.0.jar

Here is the pom for the build step ( mvn clean package )

   <build>
        <plugins>
            <!-- Overwrite the source and target JRE versions. Maven uses 1.5 which causes error during compilation -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- Package for running on server with dependencies for the `mvn package` command -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/program</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>resources</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/program/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.package.name.main.App</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>

                    <finalName>program/program-${project.version}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM