Search code examples
eclipsepathbundlercp

Including a file in the Eclipse RCP bundle working directory


I want to include a file (text, image, etc) in the root directory of an Eclipse plugin. When I run the program using a test main method, I can find the file in the working directory. But when I run the plugin as an Eclipse application, the working directory contains different files depending on the operating system and I can't find the text file. I tried adding the file to the binary build in the build tab of the xml (build.properties). It doesn't work still.

How will I find the path to the text file? How can I make sure that the text file is exported with the plugin?

Thanks


Solution

  • When you build your Eclipse plugin everything in the plugin is put in to a jar file in the 'plugins' directory. As long as your file is listed in the 'build.properties' (the build tab) it will be included in the jar.

    To access a file in the jar use:

    Bundle bundle = Platform.getBundle("your plugin id");
    
    URL url = FileLocator.find(bundle, new Path("path in plugin"), null);
    

    The URL returned is suitable for passing to various Eclipse APIs but cannot be used with normal Java APIs such as File. To convert it to a file URL use:

    URL fileURL = FileLocator.toFileURL(url);
    

    This will copy the file out of the jar in to a temporary location where it can be accessed as a normal file.

    You can also get the Bundle using:

    Bundle bundle = FrameworkUtil.getBundle(getClass());
    

    which avoids having to include the plug-in id.