views:

1308

answers:

3

Normall, this code is used for converting from File to Ifile:

IWorkspace workspace= ResourcesPlugin.getWorkspace();
IPath location= Path.fromOSString(file.getAbsolutePath());
IFile ifile= workspace.getRoot().getFilesForLocation(location);

But this only works for java files within the project. How can I get the IFile when File file is not in the workspace (not in the project)?

+3  A: 

IResource and its child IFile were originally meant to be used only for resources located in the workspace. See the Javadoc for IResource.

However, since Eclipse 3.3 there is support for external files as well, see this Eclipse Wiki page.

Zsolt Török
I tried this solution but I get this error java.lang.IllegalStateException: Workspace is closed.when I try to get the workspace, like this: IWorkspace ws = ResourcesPlugin.getWorkspace();Can you please help with this problem?
Jenny Smith
A: 

Is your code running within a plug-in that states that it requires org.eclipse.core.resources?

nitind
No, I don't want to create a plug-in at all. I just wanted to be able to use ASTParser to get some particular statements in a java file.I did a workaround, by using as source for the parser, the content of a file as a char[], instead of a IFile.
Jenny Smith
A: 

I needed this for using the Binding Resolutions from the Eclipse AST framework. But apparently, this is not possible - for anyone who need it, check this site:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=206391

Jenny Smith