maven导入jar包时,可能由于不同jar包之间依赖了不同版本的另外的jar包,会导致classnotfind异常。
举个我遇到的例子:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-netty-http</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.1.0<</version>
</dependency>
如上,我引入上面两个包时,
Channel server = NettyHttpContainerProvider.createHttp2Server(BASE_URI, new APIApplication(), null);
此时执行上面的创建启动 Netty HTTP/2 server 没有问题,正常。
由于项目中需要将hbase-client版本换成1.2.6,问题就来了。
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-netty-http</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.6<</version>
</dependency>
如上,pom中需要改为上面这样。此时在我的Activator中
Channel server = NettyHttpContainerProvider.createHttp2Server(BASE_URI, new APIApplication(), null);
会报java.lang.ClassNotFoundException: io.netty.handler.codec.http.HttpServerUpgradeHandler$UpgradeCodecFactory not found异常。
原因是1.2.6中依赖了netty-all 4.0.几的一个版本,而jersey-container-netty-http2.2.6中依赖的是netty-all 4.1.5.Final这个版本。所以需要手动引入4.1.5这个版本。
即在pom文件中加入:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.5.Final</version>
</dependency>
至此,问题解决。
问题分析步骤:
1、程序启动时抛出异常java.lang.ClassNotFoundException: io.netty.handler.codec.http.HttpServerUpgradeHandler$UpgradeCodecFactory not found,所以可以从这个类所在的jar包开始分析;
2、查找jar包所依赖的其他jar。可参考https://blog.csdn.net/qq_35893120/article/details/78436455网上这篇文章,然后找到引用的不同版本的jar, 这里就需要分析一下,看是哪个导致的classnotfind异常。 很麻烦,但我目前还不清楚有什么好的办法。(大神看到可以回复一下)
3、手动引入高版本的jar。
水平有限,出错之处 请拍砖指正。