从一个类里面找到一个jar的路径? [英] Finding the path to a jar from a Class inside it?

查看:99
本文介绍了从一个类里面找到一个jar的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

插件x.y.z应该运行在Java项目之上,并生成一些Java代码。该代码将需要在构建和运行时在Plugin的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?

我想制作一个向导,用户可以运行该向导来启用xyz自然在一个项目。应向用户提供有意义的默认位置,以查找所需的运行时功能,并将给定的库添加到构建路径。

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();
}

不知道它来自哪里。

这篇关于从一个类里面找到一个jar的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆