This section describes how to set the Java build path, i.e. the classpath used for building a Java project. A classpath is an array of classpath entries accounting for available types either in source or binary form, which are used in order to find available types. The ordering of these entries defines the precedence of the available types.
The Java build path also governs the structure of a Java project element, since all package fragment roots directly derived from the Java build path (each entry maps to one or more package fragment roots, see getPackageFragmentRoots).
This section does not cover the Java runtime path which can be defined separately, see the related section on how to run Java programs.
You can programmatically change a project's build path using
setRawClasspath on the corresponding project's Java element, for example:
IProject project = ... // get some project resource
IJavaProject javaProject = JavaCore.create(project);
IClasspathEntry[] newClasspath = ...;
javaProject.setRawClasspath(newClasspath, someProgressMonitor);
Note that there also exists a variant of setRawClasspath
which lets the Java build path and the project output location to be defined at the same time.
The Java build path is persisted into a file named '.classpath' under the project location. The purpose of this file is to provide a way to share Java build path settings with others through some source code repository. In particular, this file should not be manually edited, since it may get corrupted.
Classpath entries can be defined using factory methods defined on JavaCore, so as to reference any of the following:
e.g. entry denoting the source folder 'src' of project 'MyProject':
IClassPathEntry srcEntry = JavaCore.newSourceEntry(new Path("/MyProject/src");
e.g. entry denoting the class file folder 'lib' of 'MyProject':
IClassPathEntry libEntry = JavaCore.newLibraryEntry(
new Path("/MyProject/lib"),
null, //no source
null, //no source
false); //not exported
e.g. entry denoting an external JAR with source attachment:
IClassPathEntry libEntry = JavaCore.newLibraryEntry(
new Path("d:/lib/foo.jar"), // library location
new Path("d:/lib/foo_src.zip"), //source archive location
new Path("src"), //source archive root path
true); //exported
e.g. entry denoting a prereq project 'MyFramework', implicitly exported to further dependent projects:
IClassPathEntry prjEntry = JavaCore.newProjectEntry(new Path("/MyFramework"), true); //exported
e.g. entry denoting a library indirectly referred to using a variable 'HOME', where source attachment is also defined using variables 'SRC_HOME' and 'SRC_ROOT' :
IClassPathEntry varEntry = JavaCore.newVariableEntry(
new Path("HOME/foo.jar"), // library location
new Path("SRC_HOME/foo_src.zip"), //source archive location
new Path("SRC_ROOT"), //source archive root path
true); //exported
JavaCore.setClasspathVariable("HOME", new Path("d:/myInstall"), null); // no progress
e.g. entry denoting a system class library container:
IClassPathEntry varEntry = JavaCore.newContainerEntry(
new Path("JDKLIB/default"), // container 'JDKLIB' + hint 'default'
false); //not exported
JavaCore.setClasspathContainer(
new Path("JDKLIB/default"),
new IJavaProject[]{ myProject }, // value for 'myProject'
new IClasspathContainer[] {
new IClasspathContainer() {
public IClasspathEntry[] getClasspathEntries() {
return new IClasspathEntry[]{
JavaCore.newLibraryEntry(new Path("d:/rt.jar"), null, null, false);
};
}
public String getDescription() { return "Basic JDK library container"; }
public int getKind() { return IClasspathContainer.K_SYSTEM; }
public IPath getPath() { return new Path("JDKLIB/basic"); }
}
},
null);
It is also possible to query for the resolved classpath of a project, using getResolvedClasspath. This operation triggers initialization of involved variable(s) and/or container(s) if necessary. Note that many Java Model operations implicitly cause the Java build path to be resolved, e.g. computing a project package fragment roots requires the build path to be resolved.