加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 类路径中的Jar

发布时间:2020-12-15 04:25:10 所属栏目:Java 来源:网络整理
导读:是否有一种以编程方式获取类路径中所有jar的完整列表? 我需要这个,因为我们在两个平台之间存在行为差异. 一个由我们管理,但另一个不是,我需要知道是否存在 或者不是同一个罐子. 解决方法 Is there a programmatically way to get the complete list of all
是否有一种以编程方式获取类路径中所有jar的完整列表?

我需要这个,因为我们在两个平台之间存在行为差异.
一个由我们管理,但另一个不是,我需要知道是否存在
或者不是同一个罐子.

解决方法

Is there a programmatically way to get the complete list of all jar in
the classpath ?

这是print out the current project classpath的一种方式:

ClassLoader cl = ClassLoader.getSystemClassLoader();
java.net.URL[] urls = ((java.net.URLClassLoader)cl).getURLs();
for (java.net.URL url: urls) {
    System.out.println(url.getFile());
}

如果你需要determine which jar a class is loaded from:

Class klass = ClassInTrouble.class;
CodeSource source = klass.getProtectionDomain().getCodeSource();
System.out.println(source == null ? klass : source.getLocation());

你也可以get a list of all classes loaded in the JVM,如果它有用的话.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读