打包jar后导致找不到类的原因除了选项-classpath没配置之外
很有可能是因为打包jar后有BOOT-INF目录
需要将
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
替换成
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
如果引用了slf4j 出现找不到slf4j,是因为没有引入lib下的jar
String libPath = System.getProperty("loader.path");
log.info("lib路径:"+libPath);
File libDir = new File(System.getProperty("user.dir"), System.getProperty("loader.path"));
if (libDir.exists()){
classPath.append(libDir.getAbsolutePath()).append(File.pathSeparator);
log.info("编译需要的jar:{}", libDir.getAbsolutePath());
for (File libFile : Objects.requireNonNull(libDir.listFiles())) {
log.info("编译需要的jar:{}", libFile.getAbsolutePath());
classPath.append(libFile.getAbsolutePath()).append(File.pathSeparator);
}
}