I have 2 plugins A and B. In the MANIFEST.MF of the A I do have plugin B in a Require-Bundle section. But when I try to get B's resource from A like
ClassFromA.class.getClassLoader().getResource('resource_from_B')
I am getting null. If I put B's resourсe (folder) to A's root everything works like a charm. Am I missing something?
NOTE: I read Lars Vogel's article
Bundle bundle = Platform.getBundle("de.vogella.example.readfile");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
file = new File(FileLocator.resolve(fileURL).toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
^this solution works when I run my plugin from eclipse, but when I pack it to jar and try to install from the local update site I'm getting
java.lang.IllegalArgumentException: URI is not hierarchical
P.S. I have also read several relative questions on the StackOverflow but wasn't able to find the answer:
SOLUTION: Many thanks to @greg-449. So the correct code is:
Bundle bundle = Platform.getBundle("resource_from_some_plugin");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
URL resolvedFileURL = FileLocator.toFileURL(fileURL);
// We need to use the 3-arg constructor of URI in order to properly escape file system chars
URI resolvedURI = new URI(resolvedFileURL.getProtocol(), resolvedFileURL.getPath(), null);
File file = new File(resolvedURI);
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
NOTE: also it's very important to use the 3-arg constructor of URI in order to properly escape file system chars.
Use
FileLocator.toFileURL(fileURL)
rather than FileLocator.resolve(fileURL)
when you want to use the URL with File
.
When the plugin is packed into a jar this will cause Eclipse to create an unpacked version in a temporary location so that the object can be accessed using File
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With