我正在尝试加载一个外部 module.jar,它实现了我的主 spring 应用程序的接口(interface)。
因此我实现了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
我是否应该将我的主应用程序声明为模块中的依赖项?目前我的模块只有自己的依赖项。
我在网上找到的资源对于这个问题并不是很清楚。
最佳答案
可能是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>
关于java - ServiceLoader、URLClassLoader 和 Spring : java. lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45067222/