views:

65

answers:

2

I have a folder within my workspace but outside of 'src' which contains a file that needs to be read in order to setup a new file when using a custom plugin wizard.

I cannot get the location of this file correct and keep getting null pointers unless I specify exactly where the file is in the system. My problem is the file is within a plugin project and I cannot get it's location.

The file location in the plugin is com.my.plugin/rules/setup.txt

A: 

Assuming you have something like:

  • workspace/src/Blah.java
  • workspace/plugin/commyplugin/rules/setup.txt

From Blah.java you should be able to do something like:

URL urlToFile = getClass().getResource("/plugin/commyplugin/rules/setup.txt");

And from there create a file or use the getResourceAsStream to return a InputStream.

I'm mostly guessing here :)

willcodejavaforfood
+1  A: 

To load a resource from your deployed bundle, you can do the following (the resource to load needs to be included in your build.properties binary build setup):

Bundle bundle = YourBundleActivator.getDefault().getBundle();
IPath path = new Path("rules/setup.txt");
URL setupUrl = FileLocator.find(bundle, path, Collections.EMPTY_MAP);
File setupFile = new File(FileLocator.toFileURL(setupUrl).toURI());

Note that this is different from getting something from the workspace, as when your bundle is running, finding something in the workspace will refer to the runtime workspace, not the development workspace. If you do want something from the runtime workspace, you can access it like this:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resourceInRuntimeWorkspace = root.findMember("rules/setup.txt");
File file = new File(resourceInRuntimeWorkspace.getLocationURI());
Fabian Steeg