Home » Eclipse Projects » Rich Client Platform (RCP) » How can this be? IWorkspaceRoot exists, IProject[].length is 0
How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #443901] |
Tue, 07 February 2006 01:52 |
Eclipse User |
|
|
|
Originally posted by: jkilbour.pol.net
I am trying to access files in my workspace running the Hello RCP example,
with this bit of code added to postWindowCreate() in
ApplicationWorkbenchAdvisor:
IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
System.out.println("root exists? " + myWorkspaceRoot.exists());
IProject[] projects = myWorkspaceRoot.getProjects();
System.out.println("projects[].length "+ projects.length);
IProject myProject = myWorkspaceRoot.getProject("hello");
System.out.println("hello project exists? " + myProject.exists());
My console says:
root exists? true
projects[].length 0
hello project exists? false
|
|
| | |
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #443952 is a reply to message #443923] |
Tue, 07 February 2006 13:25 |
Eclipse User |
|
|
|
Originally posted by: jkilbour.pol.net
Thank you both; your answers clarified things I had not understood.
My objective is not necessarily to have a workspace; I wish to access
files included with the RCP from within the app. Do I need to create a
sort of "dummy project" to access these files, or is there another way?
John
|
|
|
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #443956 is a reply to message #443952] |
Tue, 07 February 2006 15:03 |
Alex Blewitt Messages: 946 Registered: July 2009 |
Senior Member |
|
|
If you want to access a file, your best bet is to include it in the plugin itself, and then use getClass().getResourceAsStream("/myfile.txt") to get an InputStream that you can read the contents for.
This won't let you write to the file, but it will give you the contents of something that you can edit externally to the code (which may be suitable for your purposes).
If you want to access a file for reading and writing, then you'll need to know where it's going to live (and why). There's a location available from the getPluginStateLocation() that's available from somewhere (can't remember off the top of my head, but it's probably from the Plugin class or that delegates to the Workspace or something ...) You can dump files in there and edit them.
Some plugins use both; for example, you might have an initial configuration file but want to let others edit it afterwards. If the file is not in the plugin state location, what you can do is copy the resource using the getClass().getResourceAsStream() method to pull the data in, then write it out to the plugin state location. Then each time the user makes a change, just save it to the state location for next time.
Alex.
|
|
| | |
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #444023 is a reply to message #443956] |
Wed, 08 February 2006 14:33 |
Eclipse User |
|
|
|
Originally posted by: jkilbour.pol.net
Alex Blewitt wrote:
> If you want to access a file, your best bet is to include it in the plugin
itself, and then use getClass().getResourceAsStream("/myfile.txt") to get an
InputStream that you can read the contents for.
The one detail that is hanging me up now in reading a file from my plugin
is the step, "including the file in the plugin".
in = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("/hello.txt")));
while ((str = in.readLine()) != null) {
System.out.println(str);
This bit of code gives me a NPE at the first line. The file "hello.txt"
is in my bin directory of Eclipse in the Plugin Development Environment,
but it looks like the file is not being found. I've set my enviroment to
not scrub output folders, and I have verified that the file does exist in
the filesysem.
|
|
|
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #444026 is a reply to message #444023] |
Wed, 08 February 2006 16:02 |
Alex Blewitt Messages: 946 Registered: July 2009 |
Senior Member |
|
|
Try without the /. I've got a funny feeling that the classloader is getting in the way and loading things from the root of the plugin, rather than the root of the class folder. Alternatively, put the 'test.txt' at the root of the project and see if that's the case.
The problem is that there seems to be subtle differences between the output folder and the plugin root depending on whether you're running in PDE or running with an exported project file. Technically, I think that if you have / then it should be the root of the *classpath* and if not it should be relative to the class folder. However, there may be other issues at work.
In any case, you'll need to ensure that 'build.properties' includes it in the 'bin.includes' list, and I have a feeling that whatever path you have in there you'll need to have in your code; so if it's in bin/myresource.txt, then build.properties needs to include 'bin/myresource.txt' with the resultant 'bin/myresource.txt' in the exported Jar; and thus you'll need to have getResourceAsStream("/bin/myresource.txt") in order to load it. PDE is a bit hacky when it comes to including non-class files that are supposed to not include the parent directory; so you can't do
/Source/OtherResources/LICENCE.TXT
and have it included in the plugin as /LICENCE.TXT. At least, you can't do that and test it in the PDE; external build tools can handle it fine. The problem comes because 'bin.includes' doesn't specify what root to start including from (in this case, /Source/OtherResources). The same problem has plagued WinZip for ages ...
My advice would be to put /myfile.txt at the plugin root (and not the bin/ folder), and access it as getREsourceAsStream("/myfile.txt"). Alternatively, stick a file called 'myfile.txt' with the contents 'This is in the root directory', 'This is in the bin directory', 'This is in the source directory'... in each file, and then do getResourceAsStream("/myfile.txt") and getResourceAsSTream("myfile.txt") to figure out which combination works, and where the file is coming from :-)
Sorry not to be of more direct help, but every time I need to do this, I end up doing something to figure out how/why PDE is working. My gut is that /myfile.txt and putting it in the plugin root is the working combo, but my brain isn't what it used to be ...
Alex.
|
|
| |
Goto Forum:
Current Time: Sat Oct 19 09:06:26 GMT 2024
Powered by FUDForum. Page generated in 0.04825 seconds
|