java ClassNotFoundException: org.apache.commons.cli.ParseException 与 maven

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32331636/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:01:51  来源:igfitidea点击:

ClassNotFoundException: org.apache.commons.cli.ParseException with maven

javamavenapache-commons-cli

提问by biology.info

I am trying to run a java project from the command line in linux :

我正在尝试从 linux 的命令行运行一个 java 项目:

$ java -jar target/my-app.jar -csv test.csv

$ java -jar target/my-app.jar -csv test.csv

and got this error

并得到这个错误

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:2531)
    at java.lang.Class.getMethod0(Class.java:2774)
    at java.lang.Class.getMethod(Class.java:1663)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException

I'm using maven-3, here my build maven configuration :

我正在使用 maven-3,这里是我的构建 maven 配置:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${appClass}</mainClass>
                            <classpathPrefix>lib/</classpathPrefix>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

And my commons-cli dependency declaration

还有我的 commons-cli 依赖声明

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

If I remove code & dependencies into my class I get no more error.

如果我将代码和依赖项删除到我的类中,我就不会再有错误了。

Thank you !

谢谢 !

回答by Garry

You are using maven but you are running the application from command line so you need to provide all the required jars to your application:

您正在使用 maven,但您正在从命令行运行应用程序,因此您需要向应用程序提供所有必需的 jars:

Approach 1: You can provide into your classpath like below:

方法 1:您可以在类路径中提供如下内容:

$ java -jar -cp "list-of-jars" target/my-app.jar -csv test.csv

If you are on Windows the path will be semi colon separated and on Linux it will colon separated. You can use wild cards also like /*.jarto include all the jars(java6+).

如果您在 Windows 上,路径将以分号分隔,而在 Linux 上,它将以冒号分隔。您可以使用通配符也喜欢/*.jar包含所有 jars(java6+)。

Approach 2: You can use one fat/uber/one jar to combine all the jars into on jar run it like you want.

方法2:您可以使用一个fat/uber/one jar 将所有jar 组合成一个jar 运行它。

Below is using one-jar:

下面是使用一个罐子:

Using Maven: you need to update the plugins section pom.xml:

使用 Maven:您需要更新插件部分 pom.xml:

<plugin>
        <groupId>org.dstovall</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <version>1.4.4</version>
        <executions>
          <execution>
            <goals>
                <goal>one-jar</goal>
            </goals>
          </execution>
        </executions>
    </plugin>

And update pluginRepositories section in pom.xml

并更新 pom.xml 中的 pluginRepositories 部分

<pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>

When you will execute the mvn packageyou will get yourappname-one-jar.jarand you can run it java -jar yourappname-one-jar.jar

当你执行mvn package你会得到yourappname-one-jar.jar并且你可以运行它java -jar yourappname-one-jar.jar

Approach 3: To use the maven shade plugin (as Robert suggested):

方法 3:使用 maven shade 插件(如 Robert 建议的):

Add this into the plugins section of pom.xml:

将此添加到 pom.xml 的 plugins 部分:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

Upon execution on mvn packagethe uber jar will be generated.

mvn packageuber jar上执行时将生成。

回答by biology.info

Using maven-dependency-plugin is a solution.

使用 maven-dependency-plugin 是一种解决方案。

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.8</version>
    <executions>
        <execution>
             <id>copy-dependencies</id>
             <phase>package</phase>
             <goals>
                 <goal>copy-dependencies</goal>
             </goals>
             <configuration>
                 <outputDirectory>${project.build.directory}/lib</outputDirectory>
             </configuration>
        </execution>
     </executions>
</plugin>

回答by Pari Rajaram

Use maven assembly plugin in your pom.xml. this will bundle all your dependencies in a single jar.

在 pom.xml 中使用 maven 程序集插件。这会将您的所有依赖项捆绑在一个 jar 中。

<plugin> 
<artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                       <mainClass>com.app.appmain</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
            </configuration>
        </plugin>
 <dependencies>
   <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.3</version>
    </dependency>
   </dependencies>

To build: mvn clean compile assembly:single

构建:mvn clean compile assembly:single

回答by amk

The -jar parameter is incompatible with -classpath or abreviatted -cp.

-jar 参数与 -classpath 或缩写的 -cp 不兼容。

So when you launch your java process using a jar file you must provide a valid Manifest.mf file which declares a correct classpath.

因此,当您使用 jar 文件启动 java 进程时,您必须提供一个有效的 Manifest.mf 文件,该文件声明了正确的类路径。

This is the manifest example from java documentation:

这是 java 文档中的清单示例:

Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)

Then you may put the .jar file inside the definitive .jar file wherever you want (then you may add the path to it). Or just leave it outside, but respect the path as if it where inside the .jar file.

然后您可以将 .jar 文件放在最终的 .jar 文件中您想要的任何位置(然后您可以添加路径)。或者只是把它放在外面,但尊重路径,就好像它在 .jar 文件中一样。