Differences

This shows you the differences between two versions of the page.

Link to this comparison view

accessing_bundle_resources [2014/02/24 14:25]
accessing_bundle_resources [2021/04/05 11:23] (current)
Line 1: Line 1:
 +====== Accessing Bundle Resources ======
 +To access a resource (file) in a bundle various solutions are possible. A discussion about how to access an included file can be found on [[http://stackoverflow.com/questions/3810303/how-to-reference-an-included-file-in-osgi-bundle-when-performing-java-io-file-or | How to reference an included file in OSGi bundle when performing java.io.File or FileInputStream]].
  
 +===== BundleContext =====
 +Accessing the resource via the BundleContext class is possible but not very convenient.
 +
 +  context.getClass.getResource("resource.file");
 +  
 +===== URL =====
 +Accessing the resource via the URL works in Eclipse but may not working in other OSGi environments.
 +
 +  URL url = new URL("platform:/plugin/my.plugin.id/folder/file.txt");
 +  InputStream inputStream = url.openConnection().getInputStream();
 +  
 +====== FileLocator ======
 +  Bundle yourBundle = Platform.getBundle("bundleSymbolicName");
 +  Path relativePathToBundle = new Path("/relativePath");
 +  FileLocator.openStream(yourBundle, relativePathToBundle, false);
 +
 +  
 +{{tag>osgi java}}