【问题标题】:ServiceLoader, URLClassLoader & Spring: java.lang.NoClassDefFoundErrorServiceLoader、URLClassLoader 和 Spring:java.lang.NoClassDefFoundError
【发布时间】:2017-12-17 10:40:40
【问题描述】:

我正在尝试加载一个外部 module.jar,它实现了我的主 spring 应用程序的接口。

因此我实现了this solution

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
    File loc = new File("plugins");

    File[] flist = loc.listFiles(new FileFilter() {
        public boolean accept(File file) {return file.getPath().toLowerCase().endsWith(".jar");}
    });

    URL[] urls = new URL[flist.length];
    for (int i = 0; i < flist.length; i++) {
        try {
            urls[i] = flist[i].toURI().toURL();
            System.out.println(flist[i].toURI().toURL().toString());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    URLClassLoader ucl = new URLClassLoader(urls);

    ServiceLoader<DeviceInterface> sl = ServiceLoader.load(DeviceInterface.class, ucl);
    Iterator<DeviceInterface> apit = sl.iterator();
    while (apit.hasNext())
        System.out.println(apit.next().getName());
}

不幸的是,这引发了这个异常:

java.lang.NoClassDefFoundError: de/maxrakete/james/device/domain/DeviceInterface

我应该在我的模块中将我的主应用程序声明为依赖项吗?目前我的模块只有自己的依赖项。

我在网上找到的资源对这个问题并不是很清楚。

【问题讨论】:

    标签: java spring serviceloader


    【解决方案1】:

    可能是springboot打包问题。尝试将重新打包添加到您的 Maven 构建中

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    【讨论】:

    • 假设我的主应用程序?
    • 不幸的是,这并没有解决我的问题。我仍然得到同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多