java -cp运行之jar包加载顺序问题(非常重要)
有没有经常在项目部署时发生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
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.
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Leetcode打卡 | No.012 整数转罗马数字
No.12 整数转罗马数字 原题: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况: I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。 X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。 给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。 示例: 示例 1: 输入: 3 输出: "III" 示例 2: 输入: 4 输出: "I...
- 下一篇
五:利用java使用redis
一:下载驱动包 http://static.runoob.com/download/jedis-2.9.0.jar 二:连接测试使用 string: 1 package string; 2 3 import redis.clients.jedis.Jedis; 4 5 public class StringTest { 6 public static void main(String[] args) { 7 Jedis redis=new Jedis("localhost");//连接本都库 8 System.out.println("ping:"+redis.ping());//ping一下看看是不是已连接 9 redis.set("stringone", "helloword");//set 10 System.out.println(redis.get("stringone"));//get 11 } 12 } list: 1 package list; 2 3 import java.util.List; 4 5 import redis.clients.jedis.Jedis...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL8.0.19开启GTID主从同步CentOS8
- Mario游戏-低调大师作品
- CentOS关闭SELinux安全模块
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Red5直播服务器,属于Java语言的直播服务器
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池