简体   繁体   中英

Getting ClassNotFoundException for class defined in external jar

I have a spring boot application app.jar that uses Apache Camel for integrating hosts. Currently, both XML routes and custom camel JAVA classes(processors) are packaged within the spring boot application. My requirement is to move XML routes and custom camel processors outside the spring boot jar so that during deployment users can write their own custom XML routes and processors. I was able to externalize XML routes using camel.springboot.routes-include-pattern = file:${workdir.path}/camel/routes/*.xml And I created a jar inside workdir/camel/libs/ (using gradle java-libray plugin) that includes all required custom camel JAVA classes. One such class is as shown below:

package com.example;

import org.apache.camel.AggregationStrategy;
import org.apache.camel.Exchange;
import org.springframework.stereotype.Component;

import com.dependencies.from.other.springboot.application.XYZ;

@Component
public class GenericParallelApiAggregationStrategy implements AggregationStrategy {

    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        //Business logic here
    }
}

Now I am trying to run the spring boot project by adding the above jar in spring boot classpath as shown below:

java -Dworkdir.path=D:/sample/workdir -cp "D:/sample/app.jar;D:/sample/workdir/camel/libs/*" org.springframework.boot.loader.JarLauncher

This is the error that I am getting:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.example.GenericParallelApiAggregationStrategy] for bean with name 'genericParallelApiAggregationStrategy' defined in URL [jar:file:/D:/sample/workdir/libs/camel-sample.jar!/com/example/GenericParallelApiAggregationStrategy.class]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/apache/camel/AggregationStrategy
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1545)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:686)
        ... 27 more
Caused by: java.lang.NoClassDefFoundError: org/apache/camel/AggregationStrategy
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        ... 38 more
Caused by: java.lang.ClassNotFoundException: org.apache.camel.AggregationStrategy
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 58 more

And this is the build.gradle file of sample-camel project:

plugins {
    id 'java-library'
}

sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot:2.5.4'
    implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.11.1'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.4'

    implementation files('D:\\sample\\app.jar')
}

Please suggest how to resolve this issue.

Seems you want to add local jars try this:

Add local jar to your module gradle (Not to the app gradle file):

    repositories {
         flatDir {
            dirs 'libs'
         }
    }

    dependencies {
       implementation name: 'app'
    }

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