开发者

Getting the src directory in an Eclipse IProject?

开发者 https://www.devze.com 2023-01-20 13:22 出处:网络
I am trying to programmatically access the src/ directory in an Eclipse project (type IProject). Basically, my problem is as follows:

I am trying to programmatically access the src/ directory in an Eclipse project (type IProject).

Basically, my problem is as follows:

  • INP开发者_开发问答UT: IProject
  • OUTPUT: Return the src directory in this IProject
  • Notes: The src directory may be called anything else (it need not be "src" every time -- it is decided when creating the java project)

Any pointers on how I can do this?


  1. Cast the IProject to IJavaProject.
  2. Get the array of IPackageFragmentRoot using getAllPackageFragmentRoots()
  3. Get the one(s) which have getKind() == IPackageFragmentRoot.K_SOURCE


The last answer did not work for me for the point number one, but following did:

IProject project = ...
if (project.isOpen() && JavaProject.hasJavaNature(project)) 
{ 
  IJavaProject javaProject = JavaCore.create(project);
  ...

}


I had the same issue, here is code:

if (project == null) return null;
    List<IJavaElement> ret = new ArrayList<IJavaElement>();
    IJavaProject javaProject = JavaCore.create(project);
    try {
            IPackageFragmentRoot[] packageFragmentRoot = javaProject.getAllPackageFragmentRoots();
            for (int i = 0; i < packageFragmentRoot.length; i++){
                if (packageFragmentRoot[i].getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT && !packageFragmentRoot[i].isArchive())
                ret.add(packageFragmentRoot[i]);
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
            return null;
        }
    return ret;
0

精彩评论

暂无评论...
验证码 换一张
取 消