Read static file in a Eclipse plugin [duplicate]

隐身守侯 提交于 2021-01-24 12:55:35

问题


I'm writing a Eclipse plugin that must read an XML file for a XSLT transformation. The XML file is located inside XSLT/ directory. I get the path in this way:

String fileXSL = "XSLT/file.xslt";

the program don't find the file. Also in this way it doesn't work:

this.getClass().getClassLoader().getResource("XSLT/file.xslt");

I've inserted this dir inside build.properties but the problem remains. Any idea?


回答1:


Use org.eclipse.core.runtime.FileLocator.

First get your Bundle:

Bundle bundle = Platform.getBundle("your plugin id");

The Bundle is also available in the BundleContext passed to the activator start method.

You can also use

Bundle bundle = FrameworkUtil.getBundle(getClass());

to get the bundle for the current class.

You can then use:

InputStream is = FileLocator.openStream(bundle, new Path("XSLT/file.xslt"), false);

or

URL eclipseURL = FileLocator.find(bundle, new Path("XSLT/file.xslt"), null);

URL fileURL = FileLocator.toFileURL(eclipseURL);

Note: The URL from FileLocator.find may use an internal Eclipse protocol so needs to be converted by FileLocator.toFileURL. This may cause the plugin jar to be unpacked in a temporary location so that a 'file' URL can be returned.



来源:https://stackoverflow.com/questions/23884611/read-static-file-in-a-eclipse-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!