Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • INPUT: 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?

like image 237
Debajit Avatar asked Oct 15 '10 21:10

Debajit


People also ask

What is src folder in eclipse?

src is where Java IDEs (at least Eclipse and NetBeans) put the source files, it is pretty much standard, and the hierarchy of the folder inside it has to match your Java packages names.

How do I reference a file in eclipse?

Files will be referenced relative to your project path, so use "resources/file. txt" to reference the file. However, if you want the file to be accessible when you export the program as a JAR, the path "resources/file. txt" must exist relative to your JAR.


2 Answers

  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
like image 130
nanda Avatar answered Sep 29 '22 07:09

nanda


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

}
like image 38
Omnaest Avatar answered Sep 29 '22 09:09

Omnaest