简体   繁体   English

获取外部 jar 中定义的类的 ClassNotFoundException

[英]Getting ClassNotFoundException for class defined in external jar

I have a spring boot application app.jar that uses Apache Camel for integrating hosts.我有一个 Spring Boot 应用程序app.jar ,它使用 Apache Camel 来集成主机。 Currently, both XML routes and custom camel JAVA classes(processors) are packaged within the spring boot application.目前,XML 路由和自定义camel JAVA 类(处理器)都打包在spring boot 应用程序中。 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.我的要求是将 XML 路由和自定义骆驼处理器移到 spring boot jar 之外,以便在部署期间用户可以编写自己的自定义 XML 路由和处理器。 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.我能够使用camel.springboot.routes-include-pattern = file:${workdir.path}/camel/routes/*.xml外部化XML 路由并且我在workdir/camel/libs/ (使用gradle java -libray 插件),包括所有必需的自定义骆驼 JAVA 类。 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:现在我试图通过在 spring boot 类路径中添加上面的 jar 来运行 spring boot 项目,如下所示:

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:这是 sample-camel 项目的build.gradle文件:

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):将本地 jar 添加到您的模块 gradle(而不是应用程序 gradle 文件):

    repositories {
         flatDir {
            dirs 'libs'
         }
    }

    dependencies {
       implementation name: 'app'
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM