- Getting Started
- Writing Java Code
- Running Java Code
- Working with Java Elements
- Tuning the Performance of JDT
- More on JDT Views and Perspectives
- Chapter Summary
Working with Java Elements
In the "Fundamentals" section earlier in this chapter, we presented an overview of how to create different kinds of Java elements. In this section we'll go into more depth on Java projects, creating and importing Java elements, and details on local history for Java elements.
More on Java Projects
Java projects add a number of properties to projects, including external tool builders, the Java build path, and the Javadoc location. We discussed Javadoc location earlier in the "Generating Javadoc" section in this chapter, and external tools builders in the section "Customizing Eclipse" in Chapter 2. A Java project's build path property specifies how JDT organizes Java code, output files, and other resources in your project.
Java Source Code Organization
Within a Java project, there are two basic ways to organize your code. For small projects, you may choose to have everything in the same package or folder, *.java source files, *.class output files, and other files required by your program. With this organization, when you save a .java file and the file and project are built (compiled), the resulting .class files are put with source files in the same folder.
For projects having lots of modules, you may choose to organize your *.java source and *.class output files in separate folders in a project. In this case, each project has one or more source folders and one output folder. The build process takes inputs from one or more source folders, or folders under them, builds them if necessary, and puts the results in the output folder. For example, when you save a .java file and the file and project are built, the resulting .class file or files are put in the output folder. Other files in the source folders are copied to the output folder, unless you specify otherwise in your Java Compiler preferences. This approach allows you to organize and subdivide your Java packages, while the output folder contains all run-time resources.
There are two preference settings that allow you to customize this source code organization and build processing. Java New Project preferences allow you to specify the default organization for when you create new Java projects. You can later override this organization on the New Project wizard's Source page. If you have already created a project, you can change this organization on the Source page of the project's Java Build Path properties. You can also change the policy for which files do not get copied to the output folder when a project is built. This is the Filtered Resources preference on the Other page of Java Compiler preferences.
Build Path
A project's build path serves two purposes: It specifies the search sequence for Java references in the code in the project, and it defines the run-time classpath for the code. When you first create a project, its build path is set to contain the project itself and the default JRE. The default JRE is defined in your Java Installed JREs preferences. At runtime, you can modify a project's classpath that was generated from its build path information by defining a launch configuration.
Classpath errors show up with other errors in the Tasks view. If you have classpath errors and have trouble diagnosing them, open the properties on the project and check the Java Build Path pages. The icons for the entries indicate if they cannot be found.
Project References
Project references indicate other projects a given project refers to and dictate how projects in your workspace are built. This property is set based on the projects you include on the Projects page of a project's Java Build Path properties. For example, if you specify in project A's Java Build Path properties that it refers to project B, project A's Project References properties are updated. Then, whenever project B is built, project A is (re)built as well, because A referenced B. You affected this when you set the build path properties for A. You do not affect this if you simply set project A's Project References properties to refer to B. Doing so will not cause A to be (re)built when B is built. The bottom line is that project references for Java projects are managed by JDT. You can examine them, but making changes will have no impact.
Creating Java Projects
Eclipse maintains information for Java projects, including build path information, the Javadoc location, and external tools builder information. You have the opportunity to set this information when you create a new project on the New Java Project wizard (see Figure 3.30). To set the information, select Next > on the first page of the New Java Project wizard rather than Finish. You can also go back later after the project is created to modify this information by editing the project's properties.
Figure 3.30 Java Build Path Properties Source Page
On the Source page, you can specify the organization of your source code and output folders. Use the project as source folder is the organization described above in which output (*.class) files are put with source (*.java) files. Use source folders contained in the project allows you to separate source from output files. If you select this, you need to specify which folders contain Java source and/or resources; otherwise, the files will not be built. Use Create New Folder... to add source folders. Use Add Existing Folders... after the project has been created to add an existing folder to the project as a source folder. The build (compile) process takes inputs from source folders you add or designate and from folders they contain. In this way, as your project grows, you can subdivide your source to make it more manageable.
The Projects page allows you to add other Java projects to the build path of the new project (see Figure 3.31). You need to do this if you are going to make references from the project you are creating to Java declarations in other projects.
Figure 3.31 Java Build Path Properties Projects Page
The Libraries page allows you to add other Java resources to your project's build path (see Figure 3.32). JDT automatically adds the default JRE. Select Add Jars... to add JAR or *.zip files from other projects, as opposed to adding references to resources in projects not contained in JARs or *.zip files, as you do on the Projects page. Generally, if you have the resources in a project, you should reference them that way, not within the JAR. Select Add External JARs... to add JAR or *.zip files from your file system. For example, if you wanted to develop a Java servlet, you would need to add servlet.jar to the project's build path. Use Add Variable... to add a classpath variable to your project's build path. The options under Advanced... allow you to add other source folders to the build path. Attach Source... allows you to add source to the selected build path entry. Do this to enable source-level debugging when the build path entry does not include source.
Figure 3.32 Java Build Path Properties Libraries Page
When you want to reference JAR files on your file system, especially if you do so in multiple projects, you should consider defining a classpath variable to refer to the JAR file and then including the variable in your projects' build paths. In this way, if the location of the JAR file changes, you only need to update the classpath variable and not the build paths of each of the projects. To define a classpath variable, use your Java preferences and select Classpath Variables.
If the build path icon is a hollow circle, it means that the library or file could not be found. Look for this when you have classpath errors.
The Order and Export page (see Figure 3.33) serves two purposes. First, it allows you to change the order of references to entries in your project's build path. This order becomes important if you have the same declarations in multiple build path entries. Second, it allows you to select an entry (that is, add a check mark) to cause its declarations to be visible outside the project.
Figure 3.33 Java Build Path Properties Order and Export Page
Creating Folders
|
There are two types of folders in Java projects: Source Folders and Nonsource ("simple") Folders. This is an important distinction because it affects how builds are performed. When you create a source folder, JDT adds the source folder to the project's build path. Nonsource folders are not added to the project's build path. If you create a folder and later want to make it a source folder, do so from the Java Project Properties dialog, Java Build Path properties, on the Source page. When in doubt, check the project's .classpath file. You can put nonsource folders in Java projects and folders; you can put source folders only in Java projects.
If you create a Java project and select to have your Java source and output files in the same folder, and then decide later to create a source folder, this forces JDT to change the organization of the project to have separate source and output folders. If you already have Java files in the project, you will have to move these manually to a source folder. It may be easier to create a new project with the folder and package organization you desire, and then use the Refactoring actions to move the Java elements.
Creating Classes and Interfaces
The options available when creating a new class or interface are straightforward. Several are worth noting. Enclosing Type is used for nested classes and specifies the class that will contain the class you are creating. As you type the Name of the class, watch the message at the top of the wizard pageit will flag invalid class names. An easy way to create a subclass is to select a class in one of the Java views, and then select to create a new class. The Superclass of the new class is the one you had selected. If you elect to not generate stubs for superclass constructors or inherited methods, you can later generate the code from the editor by selecting Source > from the context menu, or by using content assist and a code template. If you do not select to have Inherited abstract methods generated, you will see errors in the Tasks view indicating the methods you still need to create. Once the class is created, you can generate stubs for these by selecting the class in one of the Java views and then selecting Override Methods... from the context menu.
Importing Java Elements
In the section "Resource Management" in Chapter 2, we discussed importing resources with the Import wizard. Recall that you see the Import wizard by selecting Import... from the Navigator or Package Explorer context menu or by selecting File > Import.... With Java projects and Java elements, there are additional considerations. Generally, it is easier to deal with projects than individual Java elements because Java projects contain build path and tool builder information in the project's .classpath and .project files. When you select to import Existing Project into Workspace in the Import wizard, a folder structure is a valid Eclipse project if it includes a .project file. Recall that importing an existing project into your workspace does not cause the contents of the project to be moved into your workspace folder. Rather, a definition for the project is added with a reference to its existing location. So, if you import an existing project from one workspace to another and you don't intend to work on it further in the original workspace, be sure to delete the project definition there, but not the contents. This would delete the project's contents for both workspaces.
The easiest way to import and export code and in general share it is to do so with an SCM repository and by means of a Team Project Set. We'll see this in Chapter 5.
Local History for Java Elements
The section "Resource Management" in Chapter 2 described how you could compare and replace a file with another edition of it from local history, compare two files, compare two projects, and recover files deleted from a project. In addition to this, with JDT you can compare and replace individual elements, including methods and fields. You can select an element in one of the Java views and then select Compare With >, Replace With >, or Restore from Local History... from the context menu. Figure 3.34 shows comparing a single method, generatePrimeNumbers(), with a previous edition.
Figure 3.34 Comparing a Java Element from Local History
When you compare Java elements, you can select an element in the Java Structure Compare pane to see only its differences. You can restore a deleted method or field by selecting a type in the Outline view and then selecting Restore from Local History from the context menu.