Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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] Mon, 06 February 2006 20:52 Go to next message
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 #443919 is a reply to message #443901] Tue, 07 February 2006 05:59 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex Blewitt
Messages: 946
Registered: July 2009
Senior Member
I suspect I've already answered this in your other question, but for those that pick up this thread in a search; the answer is that the workspace is a different workspace from the runtime-workspace that's used for testing plugins. When you do Run on an Eclipse PDE environment, it creates a new workspace which is completely empty. So unless you do something with it, it won't have any projects in.

Either you can create a new project in the runtime-workspace, or you can import one that's in yours using File->Import->Existing project into workspace. If you're running with the defaults of everything, that should be '../workspace/ProjectName'.

Bear in mind that if you run with the 'clean workspace on startup' option (which is probably a good idea) then it will effectively reset the project count to zero, and you'll have to go through this process again.

Alex.
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #443923 is a reply to message #443901] Tue, 07 February 2006 07:09 Go to previous messageGo to next message
DJ Houghton is currently offline DJ Houghton
Messages: 28
Registered: July 2009
Junior Member
The IProject that you are getting from the root is just a handle. Think
of it like java.io.File. So it doesn't exist yet. (as is the case for
all the types in the IResource hierarchy)

If you want to create a project (optionally get a new project
description from the IWorkspace and) call #create on the IProject handle
that you already have.

A reminder that the project will have to be opened to do anything useful
to it. (IProject#open)

John Kilbourne wrote:
> 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 08:25 Go to previous messageGo to next message
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 10:03 Go to previous messageGo to next message
Alex Blewitt is currently offline 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 #443957 is a reply to message #443919] Tue, 07 February 2006 10:22 Go to previous messageGo to next message
david is currently offline david
Messages: 61
Registered: July 2009
Member
Hello,

Is there any complete and good tuts on how to use IResource etc... caus i'd like to developp a rcp standalone application that works a bit like eclipse, so in my application i can create projects, folders and import files from the system and the all is displayed in a tree viewer, i started it making a model that extend java.io.File but i'm having problems for renaming cutting and so, and i think that i should use IResource, IProject, IFolder and IFile in my model instead of instance of java.io.File but i don't really know how the thing works so i there's any good tuts please indicate it to me..
Thank you

David
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #443961 is a reply to message #443957] Tue, 07 February 2006 13:23 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex Blewitt
Messages: 946
Registered: July 2009
Senior Member
If you include the org.eclipse.core.resources project, you get the concept of IResources et al with it. The viewers are in org.eclipse.ui.views, and you'll find that the Navigator view is probably in the org.eclipse.ide.ui plugin; you just need to add that to your RCP mix and you'll see the navigator in there.

As for tutorials; http://www.eclipse.org/articles/ is the best place to look for them. There's one on resource changes (which tells you waht's changed) and that obviously uses some of the resource stuff:

http://www.eclipse.org/articles/Article-Resource-deltas/reso urce-deltas.html

There's also stuff in the help pages about it. I suggest that you also read the article on IAdaptable, since a lot of the frameworks (especally the IResource classes) use it to return different types, as opposed to a Java cast.

http://www.eclipsezone.com/articles/what-is-iadaptable/?sour ce=archives

Alex.
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #444023 is a reply to message #443956] Wed, 08 February 2006 09:33 Go to previous messageGo to next message
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 11:02 Go to previous messageGo to next message
Alex Blewitt is currently offline 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.
Re: How can this be? IWorkspaceRoot exists, IProject[].length is 0 [message #522295 is a reply to message #444026] Mon, 22 March 2010 03:30 Go to previous message
lehmia  is currently offline lehmia
Messages: 29
Registered: February 2010
Junior Member
I am getting the same error, i try to import the project in the workspace by using File->Import->Existing project into workspace it was already there in work space and import is disabled, i again run my rcp application .still the length of the projects in workspace is zero.

Any help???
Previous Topic:How load text into JavaStackTraceConsolePage programmatically?
Next Topic:ExceptionInitializerError when deploying eclipse product
Goto Forum:
  


Current Time: Wed Aug 20 20:32:31 EDT 2014

Powered by FUDForum. Page generated in 0.01837 seconds