簡體   English   中英

ServiceLoader,URLClassLoader和Spring:java.lang.NoClassDefFoundError

[英]ServiceLoader, URLClassLoader & Spring: java.lang.NoClassDefFoundError

我正在嘗試加載一個外部module.jar,該模塊從我的主spring應用程序實現一個Interface。

因此,我實施了此解決方案

@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

我是否應該在模塊中將主應用程序聲明為依賴項? 目前,我的模塊只有其自己的依賴項。

我在網上發現的解決方案對此問題並不十分清楚。

可能是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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM