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 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);