Debajit
Debajit

Reputation: 47111

Getting the src directory in an Eclipse IProject?

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

Basically, my problem is as follows:

Any pointers on how I can do this?

Upvotes: 5

Views: 2731

Answers (3)

Mindaugas Jaraminas
Mindaugas Jaraminas

Reputation: 3437

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;

Upvotes: 0

Omnaest
Omnaest

Reputation: 3096

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);
  ...

}

Upvotes: 2

nanda
nanda

Reputation: 24788

  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

Upvotes: 8

Related Questions