问题
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