Maven: NoClassDefFoundError in the main thread

后端 未结 2 1967
Happy的楠姐
Happy的楠姐 2020-12-14 17:33

I am currently building a little Apache-Mina Server app. I am using Maven to build it. When i try to run the jar, I get the following error:

    Exception in         


        
相关标签:
2条回答
  • 2020-12-14 17:50

    When you run from Eclipse, Eclipse configures the class path for you. Therefore, you don't run into this issue.

    When you are running outside of Eclipse, you need to set up the CLASSPATH either by providing the path to these jar files ie file:/dev/libs/mina-core-2.0.3.jar in the MANIFEST.MF or by adding the -cp option when executing the app. Don't forget that the entries in the class-path in the manifest file are either relative to the JAR in which they are embedded or absolute path to a local file directory.

    Your other option is to package it as one jar using the maven assembly plugin jar-with-dependencies.

    0 讨论(0)
  • 2020-12-14 18:07

    Another option is to use maven-dependency-plugin. You can copy all dependent libraries to a folder such as lib, and use that for classpath.

    In order to copy dependencies:

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

    and for the classpath, here classpathPrefix specifies that all dependencies should be located in a "lib" folder relative to the archive.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>com.citusdata.hadoop.HadoopTest</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    

    For further information:

    http://www.ibm.com/developerworks/java/library/j-5things13/index.html http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

    0 讨论(0)
提交回复
热议问题