简体   繁体   中英

java.lang.NoClassDefFoundError jar application

I got a maven project with Netbeans 8 (linux).

My project is a maven application that I have to 'export' as a JAR runnable JAVA application. While I'm developing the system in Netbeans everything works fine with maven dependencies, but after compile and generate the .jar file in the target folder I got the error NoClassDefFoundError. In my searches through google I found that this problem is caused when I have the dependencies in development environment but not when it's compiled with classpath.

One solution is to include dependencies jar as library in project, but with this I lost maven functionality. I don't wanna make it, just in last case.

How can I solve this problem without add dependencies as library inside project?

Thank you very much!

You do not use the correct maven plugin to generate a runnable JAR, meaning a jar with all dependencies within it. Here is how it should look like

<plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <finalName>.....</finalName>
        <outputDirectory>${basedir}</outputDirectory>
        <archive>
        <manifest>
            <mainClass>......</mainClass>
        </manifest>
        </archive>
        <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
        <phase>package</phase>
        <goals>
            <goal>single</goal>
        </goals>
        </execution>
    </executions>
    </plugin>
</plugins>

What you have configured is the standard maven plugin to generate a jar file, but without the dependencies included.

What is generating error is the Hibernate and the POI.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.print.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>unknown-jars-temp-repo</id>
        <name>"mvn deploy:deploy-file -Durl=file:///var/www/projetos/041print-proc/lib/ -Dfile=hibernate-core-4.3.10.Final.jar -DgroupId=org.hibernate -DartifactId=hibernate-core -Dpackaging=jar -Dversion=4.3.10.Final"</name>
        <url>file:${project.basedir}/lib</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1201-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.9</version>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<name>041 Print Processadores</name>
<description>Processadores de Texto para Dados Variáveis</description>

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