Description
IDEA开发时启动调试一切正常 | 使用maven打成jar包后调用出现异常信息
java.lang.NoClassDefFoundError: Could not initialize class com.baidu.bjf.remoting.protobuf.utils.JDKCompilerHelper
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.doCreate(ProtobufProxy.java:263)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:174)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:155)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:128)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:116)
at com.iohao.game.common.kit.ProtoKit.lambda$create$0(ProtoKit.java:91)
at java.base/java.util.concurrent.ThreadPerTaskExecutor$TaskRunner.run(ThreadPerTaskExecutor.java:314)
at java.base/java.lang.VirtualThread.run(VirtualThread.java:329)
java.lang.ExceptionInInitializerError
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.doCreate(ProtobufProxy.java:263)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:174)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:155)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:128)
at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:116)
at com.iohao.game.common.kit.ProtoKit.lambda$create$0(ProtoKit.java:91)
at java.base/java.util.concurrent.ThreadPerTaskExecutor$TaskRunner.run(ThreadPerTaskExecutor.java:314)
at java.base/java.lang.VirtualThread.run(VirtualThread.java:329)
Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\C:\Users\Administrator\Desktop\JavaProject\external0.0.1.jar!\BOOT-INF\classes!
at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:204)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:175)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:231)
at java.base/java.io.File.toPath(File.java:2401)
at jdk.compiler/com.sun.tools.javac.file.JavacFileManager$2.next(JavacFileManager.java:1304)
at jdk.compiler/com.sun.tools.javac.file.JavacFileManager$2.next(JavacFileManager.java:1294)
at jdk.compiler/com.sun.tools.javac.file.Locations$SearchPath.addFiles(Locations.java:348)
at jdk.compiler/com.sun.tools.javac.file.Locations$SearchPath.addFiles(Locations.java:356)
at jdk.compiler/com.sun.tools.javac.file.Locations$SimpleLocationHandler.setPaths(Locations.java:728)
at jdk.compiler/com.sun.tools.javac.file.Locations.setLocation(Locations.java:2156)
at jdk.compiler/com.sun.tools.javac.file.JavacFileManager.setLocation(JavacFileManager.java:992)
at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.<init>(JdkCompiler.java:166)
at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.<init>(JdkCompiler.java:103)
at com.baidu.bjf.remoting.protobuf.utils.JDKCompilerHelper.<clinit>(JDKCompilerHelper.java:33)
... 8 more
系统环境
windows10、liberica jdk 21.0.6
出现的现象
IDEA中开发时启动调试没有任何异常,使用spring-boot-maven-plugin插件打成jar包后使用Java -jar执行会出现以上信息,倒是客户端能够连接上,但是无法进行数据通信,有以下现象:
1、只在windows环境下出现,经测试linux和mac os系统中无异常。
2、只使用jar执行会出异常,IDEA中没有异常情况。
3、只有使用了ExternalServer对外服的项目模块会出现,BrokerServer网关服和BarSkeleton逻辑服的不会出现异常情况。
异常抛出的方法调用位置大致如下
com.iohao.game.common.kit.ProtoKit.create()
└──── com.baidu.bjf.remoting.protobuf.ProtobufProxy.create()
再下面就是jprotobuf的代码了
复现步骤
使用\ioGameExamples\SpringBootExample\spring-game-external提供的demo就可以复现出来
可行解决方案
将maven的打包插件由spring-boot-maven-plugin更改为maven-assembly-plugin就不会出现这种情况了。
官方文档中额外注明了spring项目需要使用spring-boot-maven-plugin插件进行打包
https://www.yuque.com/iohao/game/ruaqza
└──── 打包、部署 -> 注意:如果是 spring 项目,需要使用 spring-boot-maven-plugin 代替 maven-assembly-plugin ,具体查看 issu。
分析
应该是使用spring-boot-maven-plugin打出的jar包内部会存在一个BOOT-INF目录来管理引用的依赖,然后jprotobuf读取到了这个目录所以出问题了,但是使用maven-assembly-plugin打出的jar包不会有这个目录,不确定有没有什么影响。
而且发现有的项目的pom.xml使用maven-assembly-plugin会出现编译不通过,但是使用spring-boot-maven-plugin可以打包,不知道能不能处理spring-boot-maven-plugin的打包问题?
版本
- ioGame 21.24: