问题
外部下载的jar包在本地能运行,但是打成jar包之后找不到这个下载的外部jar包
报错信息
Error creating bean with name ‘loginController’
这里指向LoginController类的问题
查看caused by
- Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.bootadmin1.bootajaxtest.controller.LoginController] from ClassLoader [org.springframework.boot.loader.LaunchedURL
- Caused by: java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSONObject
- Caused by: java.lang.ClassNotFoundException: com.alibaba.fastjson.JSONObject
定位
fastJson是导入的外部jar,这里找不到证明打包的时候没有把外部jar包导入进去,所以在纠错之后使用如下完整的导入方法
pom.xml
注意依赖和插件是加在不同地方的
依赖
<!--maven打包插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.3</version>
</dependency>
<!--maven默认插件,用于编译源码-->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
插件
忽视lombok插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
解释:
-
spring-boot-maven-plugin是打包插件,引入这个插件之后用maven打包出来的jar包才是可执行的完整jar包
-
而includeSystemScope>true</includeSystemScope是设置打包的时候是否包括外部引入的jar包
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
外部引入jar包
这里以阿里的fastJson为例
同时,需要在pom中引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/main/resources/lib/fastjson-1.2.75.jar</systemPath>
</dependency>
注意:
- scope>system</scope必须设置system,这里和上面的includeSystemScope>true</includeSystemScope呼应
- systemPath>${pom.basedir}/src/main/resources/lib/fastjson-1.2.75.jar</systemPath中的路径必须指明引入的jar包
clean package
打包之后,点开jar包
搜索lib