Search code examples
osgibundlekaraf

Get relative path to a folder inside OSGI Bundle


I've developed a sample OSGI bundle and deployed it into Karaf, and the bundle contains a folder on the same level as the source.

My class needs to load data from this folder, but I got an error each time:

java.lang.IllegalArgumentException: No such group file: ./data/...

My question is: how do I use a relative path to a folder inside an OSGI Bundle?


Solution

  • solved using context from bundle:

    URL wfl = context.getBundle().getResource("data1/file.txt");
    

    or

    URL url = MyClass.class.getClassLoader().getResource("data1/file.txt");
                if (url != null) {          
                    mFile = FileLocator.toFileURL(url).getFile();
    

    Hope this help someone