How to get a resource from another plugin?

I have 2 plguins A and B. In MANIFEST.MF A , I have plugin B in the Require-Bundle section. But when I try to get resource B from A , for example

ClassFromA.class.getClassLoader().getResource('resource_from_B') 

I get null . If I put a B resource (folder) in A , everything works like a charm. Am I missing something?

NOTE. I am reading a Lars Vogel 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 in jar and try to install from the local update site, I get

 java.lang.IllegalArgumentException: URI is not hierarchical 

PS I also read a few relative questions in StackOverflow, but couldn't find the answer:

  • Java Jar file: use resource errors: URI is not hierarchical
  • Why is my URI not hierarchical?

SOLUTION: Thank you very much @ greg-449. Therefore, 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 important use the 3-arg URI constructor to properly remove file system characters.

+7
eclipse eclipse-plugin eclipse-rcp
source share
2 answers

Use

 FileLocator.toFileURL(fileURL) 

not FileLocator.resolve(fileURL) if you want to use a URL with File .

When the plugin is packaged in a jar, this will force Eclipse to create the unpacked version at a temporary location so that the object can be accessed using File .

+5
source share

Why not use Bundle.getResource ? This is similar to OSGi's way of accessing resources.

Your first attempt may work if you use ClassFromB.class.getClassLoader instead of A

0
source share

All Articles