您现在的位置是:首页 > 文章详情

java -cp运行之jar包加载顺序问题(非常重要)

日期:2018-06-28点击:506

有没有经常在项目部署时发生java.lang.NoSuchMethodError、java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的错误(告诉你,一般都是jar包冲突了),那么怎样排查呢?排查到jar冲突后又怎样解决呢?

(非常重要的特:java -cp 是有顺序的!!!)

1. jar冲突故障排查:

以springboot工程为例,只需在使用@SpringBootApplication的入口类上加上:

Class<?> c = Class.forName("org.aa.bb.XXClass");

System.out.println(c.getProtectionDomain().getCodeSource().getLocation())

然后启动,查看打印的jar路径,即就是当前实际加载的那个包下的类。

2. jar加载顺序举例说:

a.jar, b.jar 都有 MyTest这个类。

java -cp a.jar:b.jar

那么 这个MyTest 拿到的到底 是哪个jar包的?

答案是:cp 前面的那个(经验所知,springboot扫描加载yml、properties文件也是同理,排在前面的classpath被加载,后面的将不会被加载)。

怎么解决呢?

若是使用shell脚本添加的classpath,可参考:关于shell sort使用

在关键shell代码中需要排序`CLASSPATH`字符串:

# classpath addition for release.

for file in `ls -a "$base_dir"/libs/* | sort -r`;

do

    CLASSPATH="$CLASSPATH":"$file"

done


Oracle官网关于cp顺序说明

Specification Order

The order in which you specify multiple class path entries is

important. The Java interpreter will look for classes in the

directories in the order they appear in the class path variable. In

the previous example, the Java interpreter will first look for a

needed class in the directory /java/MyClasses. Only when it does not

find a class with the proper name in that directory will the

interpreter look in the /java/OtherClasses directory.

原文链接:https://yq.aliyun.com/articles/616317
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章