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.
source share