File Search in IProject - Eclipse

I need to find the specific file present in the eclipse project which is in the classpath of the project.

I have an IProject instance but don't know how to get an IFile instance

+4
source share
2 answers

The IProject interface extends IContainer , which has several findMember methods. You get an IResource that can be reset to IFile after checking its type with getType . Go through these interfaces, they are correctly documented.

+6
source

I ran into the same problem and this is briefly what I did:

 IResource getResource(IProject project, String folderPath, String fileName) { IJavaProject javaProject = JavaCore.create(project); for (IPackageFragmentRoot root : javaProject.getAllPackageFragmentRoots()) { IPackageFragment folderFragment = root.getPackageFragement(folderPath); IResource folder = folderFragment.getResource(); if (folder == null || ! folder.exists() || !(folder instanceof IContainer)) { continue; } IResource resource = ((IContainer) folder).findMember(fileName); if (resource.exists()) { return resource; } } // file not found in any source path return null; } 

It looks pretty ugly and there may be a more direct approach. But it works.

If you need to use the class path, you must use IJavaProject , and working with fragments prevents a direct search for the path to the file, because it will take the value ".". (period) in the file name is the java package separator. Therefore, I think you should first get the desired folder, and then the file.

+3
source

All Articles