Finding the path to a jar from a Class inside it?(从里面的 Class 中找到 jar 的路径?)
问题描述
插件 x.y.z 应该在 Java 项目之上运行并生成一些 Java 代码.此代码将需要在构建和运行时插件 jar 中可用的类.因此,插件的 jar(或安装目录)应该出现在构建类路径中.
Plugin x.y.z is supposed to run on top of a Java project and generate some Java-Code. This code will need classes available in the Plugin's jar at build and run time. Hence, the Plugin's jar (or installation directory) should appear in the build classpath.
插件如何以可移植的方式找到它自己的 jar/安装目录的确切路径,或者就此而言,某个关联插件的 jar 的路径?
How can a plugin find out the exact path of it's own jar/installation directory, or, for that matter, the path to the jar of some associated plugin in a portable way?
背景是我想制作一个用户可以运行以启用 x.y.z 的向导.项目的性质.应该为用户提供一个有意义的默认值,以便在哪里可以找到所需的运行时功能,并且给定的库将被添加到构建路径中.
Background is I want to make a wizard that the user can run to enable x.y.z. nature on a project. The user should be provided with a meaningful default for where to find the required runtime functionality, and the given library will be added to the build path.
推荐答案
我们用这个来查找类的位置:
We use this to find the location of a class:
public static URL getLocation(final Class cls) {
final ProtectionDomain pd = cls.getProtectionDomain();
final CodeSource cs = pd.getCodeSource();
return cs.getLocation();
}
不确定它来自哪里.
这篇关于从里面的 Class 中找到 jar 的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从里面的 Class 中找到 jar 的路径?


- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01