【问题标题】:Getting ClassNotFoundException for class defined in external jar获取外部 jar 中定义的类的 ClassNotFoundException
【发布时间】:2021-10-27 13:21:56
【问题描述】:

我有一个 Spring Boot 应用程序 app.jar,它使用 Apache Camel 来集成主机。 目前,XML 路由和自定义骆驼 JAVA 类(处理器)都打包在 Spring Boot 应用程序中。 我的要求是将 XML 路由和自定义骆驼处理器移到 spring boot jar 之外,以便在部署期间用户可以编写自己的自定义 XML 路由和处理器。 我能够使用camel.springboot.routes-include-pattern = file:${workdir.path}/camel/routes/*.xml 将 XML 路由外部化 我在workdir/camel/libs/(使用 gradle java-libray 插件)中创建了一个 jar,其中包含所有必需的自定义骆驼 JAVA 类。一个这样的类如下所示:

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
    }
}

现在我正在尝试通过在 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

这是我得到的错误:

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

这是 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')
}

请建议如何解决此问题。

【问题讨论】:

    标签: java spring spring-boot apache-camel spring-camel


    【解决方案1】:

    似乎你想添加本地 jar 试试这个:

    将本地 jar 添加到您的模块 gradle(而不是应用程序 gradle 文件):

        repositories {
             flatDir {
                dirs 'libs'
             }
        }
    
        dependencies {
           implementation name: 'app'
        }
    

    【讨论】:

    • 不,向 jar 添加本地依赖不是问题。库项目正在编译。只有当我们尝试使用类路径中的库 jar 运行 Springboot 应用程序时,我才会收到错误消息。
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多