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 的路径?


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01