如何通过swt / java编程获取Eclipse安装目录。 我其实想要得到eclipse的插件目录。
答案 0 :(得分:8)
更新(2012年1月)
James Moore在the comments中提到常见问题解答和API已经过时了。
FileLocator.resolve(URL)
现在优先于已弃用的Platform.resolve()
。
如this example所示,您需要传递实际资源(此处为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;
}
}
有关详情,另请参阅“How to refer a file from jar file in eclipse plugin”。
原始答案(2011年1月)
也许常见问题解答“How do I find out the install location of a plug-in?”可以在这里提供帮助:
通常应避免在运行时假设插件的位置 要查找存储在插件安装目录中的资源(如图像),可以使用
Platform
类提供的URL。这些URL使用特殊的Eclipse Platform协议,但如果您只使用它们来读取文件,则无关紧要。在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();
如果您需要知道插件的文件系统位置,则需要使用
Platform.resolve(URL)
。此方法将平台URL转换为标准URL协议,例如超文本传输协议(HTTP)或文件 请注意,Eclipse Platform没有指定插件必须存在于本地文件系统中,因此您不能依赖此方法在将来的所有情况下返回文件系统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();
答案 1 :(得分:0)
使用以下代码获取插件路径:
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();