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

Aswin
  • 541
  • 4
  • 13

1 Answers1

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Sorry for taking so long to reply. I will try this out and accept your answer. Thank you for the help. – Aswin Sep 19 '14 at 02:23
  • That worked beautifully. Thanks for the solution and the clear explanation of how it works. – Aswin Dec 15 '14 at 19:54