Jump to: navigation, search

FAQ How do I create a Java project?

Revision as of 16:38, 14 March 2006 by Claffra (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)




Several steps are required to create and properly initialize a Java project. Start by creating and opening an IProject:

   String name = "MyProject";
   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   IWorkspaceRoot root= workspace.getRoot();
   IProject project= root.getProject(name);
   project.create(null);
   project.open(null);

Next, you need to add the Java nature to the project. This in turn will cause the Java builder to be added to the project:

   IProjectDescription desc = project.getDescription();
   desc.setNatureIds(new String[] {
      JavaCore.NATURE_ID});
   project.setDescription(desc);

Next, you must set the Java builder’s output folder, typically called bin. This is where the Java builder will place all compiled *.class files:

   IJavaProject javaProj = JavaCore.create(project);
   IFolder binDir = project.getFolder("bin");
   IPath binPath = binDir.getFullPath();
   javaProj.setOutputLocation(binPath, null);

Finally, you need to set the project’s classpath, also known as the build path. You will need to minimally create a classpath entry that points to the Java runtime library, rt.jar, and additional entries for any other libraries and projects that the project requires:

   String path = "c:\\jre\\lib\\rt.jar";
   IClasspathEntry cpe= JavaCore.newLibraryEntry(
      path, null, null);
   javaProj.setRawClasspath(new IClasspathEntry[] {cpe});

See Also:

FAQ_When_does_PDE_change_a_plug-in’s_Java_build_path?

FAQ_How_are_resources_created?

FAQ_Why_should_I_add_my_own_project_nature?


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.