以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.