繁体   English   中英

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

[英]Getting ClassNotFoundException for class defined in external jar

我有一个 Spring Boot 应用程序app.jar ,它使用 Apache Camel 来集成主机。 目前,XML 路由和自定义camel 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 插件),包括所有必需的自定义骆驼 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')
}

请建议如何解决此问题。

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

将本地 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