简体   繁体   中英

Importing external JAR with NoClassDefFoundError - depedencies

I've to use in my project a library provided as .jar file by another team. However, when I'm running code using this library I'm getting NoClassDefFoundError . I've made some investigation and I know that it could be avoided if the external team will create the jar with all needed dependencies on the classpath (eg lib folder). So my understanding is that then I can import mentioned library and all needed sub-jars will be in folder lib. The questions: Am I thinking correctly? Should I ask another team to provide lib with all sub-jar dependencies? Is there any alternative for this solution without providing jar with all dependencies inside, which allows avoiding this kind of errors?

If you can compile your project with maven, then there must be all the jar files needed. If you only pick your "jar" file and execute it, it will fail, because you must include the dependencies in the classpath.

Another option is to build the jar file including all the dependencies in it.

For example, using the maven-assembly-plugin :

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.path.to.your.MainClass</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <appendAssemblyId>false</appendAssemblyId>        
    </configuration>
</plugin>

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