eclipse 如何获取eclipse安装/插件目录或路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4720214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the eclipse installation/plugins directory or path
提问by Abhishek Choudhary
How to get the Eclipse installation directory through programming in swt/java. I actually want to get the plugins directory of the eclipse.
如何在swt/java中通过编程获取Eclipse安装目录。我其实是想获取eclipse的plugins目录。
回答by VonC
Update (January 2012)
James Moorementions in the commentsthat the FAQ and API are quite old.
更新(2012 年 1 月)
James Moore在评论中提到FAQ 和 API 已经很老了。
FileLocator.resolve(URL)
is now preferred to the deprecated Platform.resolve()
.
FileLocator.resolve(URL)
现在优先于已弃用的Platform.resolve()
.
As this exampleshows, you need to pass the actual resource (here a bundle), not the name of the resource, in order to resolve it:
如本例所示,您需要传递实际资源(此处为bundle),而不是资源名称,以便解析它:
private static URI locateFile(String bundle, String fullPath) {
try {
URL url = FileLocator.find(Platform.getBundle(bundle), new Path(fullPath), null);
if(url != null)
return FileLocator.resolve(url).toURI();
} catch (Exception e) {}
return null;
}
}
See also "How to refer a file from jar file in eclipse plugin" for more.
另请参阅“如何在 eclipse 插件中从 jar 文件中引用文件”了解更多信息。
Original answer (January 2011)
原始答案(2011 年 1 月)
Maybe the FAQ "How do I find out the install location of a plug-in?" can help here:
也许常见问题解答“如何找出插件的安装位置?”可以在这里提供帮助:
You should generally avoid making assumptions about the location of a plug-in at runtime.
To find resources, such as images, that are stored in your plug-in's install directory, you can use URLs provided by thePlatform
class. These URLs use a special Eclipse Platform protocol, but if you are using them only to read files, it does not matter.In Eclipse 3.1 and earlier, the following snippet opens an input stream on a file called
sample.gif
located in a subdirectory, calledicons
, of a plug-in's install directory:
您通常应该避免在运行时对插件的位置做出假设。
要查找存储在插件安装目录中的资源(例如图像),您可以使用Platform
该类提供的 URL 。这些 URL 使用特殊的 Eclipse 平台协议,但如果您仅使用它们来读取文件,则没有关系。在 Eclipse 3.1 及更早版本中,以下代码段在
sample.gif
位于icons
插件安装目录的子目录中的名为的文件上打开输入流:
Bundle bundle = Platform.getBundle(yourPluginId);
Path path = new Path("icons/sample.gif");
URL fileURL = Platform.find(bundle, path);
InputStream in = fileURL.openStream();
If you need to know the file system location of a plug-in, you need to use
Platform.resolve(URL)
. This method converts a platform URL to a standard URL protocol, such as HyperText Transfer Protocol (HTTP), or file.
Note that the Eclipse Platform does not specify that plug-ins must exist in the local file system, so you cannot rely on this method's returning a file system URL under all circumstances in the future.In Eclipse 3.2, the preferred method seems to be:
如果您需要知道插件的文件系统位置,则需要使用
Platform.resolve(URL)
. 此方法将平台 URL 转换为标准 URL 协议,例如超文本传输协议 (HTTP) 或文件。
请注意,Eclipse 平台并未指定插件必须存在于本地文件系统中,因此您以后不能在任何情况下都依赖此方法返回文件系统 URL。在 Eclipse 3.2 中,首选方法似乎是:
Bundle bundle = Platform.getBundle(yourPluginId);
Path path = new Path("icons/sample.gif");
URL fileURL = FileLocator.find(bundle, path, null);
InputStream in = fileURL.openStream();
回答by V_Dev
Use the below code to get the plugin path :
使用以下代码获取插件路径:
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();