Including a file in the Eclipse RCP bundle working directory
Asked Answered
D

1

5

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

Dexter answered 19/8, 2014 at 23:23 Comment(0)
C
8

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.

Chemist answered 20/8, 2014 at 7:18 Comment(2)
Sorry for taking so long to reply. I will try this out and accept your answer. Thank you for the help.Dexter
That worked beautifully. Thanks for the solution and the clear explanation of how it works.Dexter

© 2022 - 2024 — McMap. All rights reserved.