4

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

2
  • I don't know about plugin programming, but if you want to access programmiatically something you have to put it inside the classpath, it is, it has to be inside a source folder. You can have more than one if necesary (by example, the Maven style project structure, contains at least two folders, one for java code, and one for resources, it is, files available at runtime). Commented Jan 27, 2010 at 17:16
  • I'll explain myself better: in any Java project you have to program something to work when it's compiled. So you have available what's packaged in your final product. Your java files will be compiled and available. If you need static files you'll need them to be inside some source folder, be it /src or not. You can moev the file to /src or reconfigure the Java Build Path | Source, in project properties. Commented Jan 27, 2010 at 17:19

2 Answers 2

7

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()); 
Sign up to request clarification or add additional context in comments.

Comments

0

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 :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.