Eclipse中的Java Project Build Path

本文详细介绍了如何在Eclipse中进行Java项目的配置,包括设置源文件夹和输出文件夹、配置项目依赖、添加库到构建路径等。还介绍了如何创建用户库、覆盖工作空间编译器偏好设置等内容。

      在网上看到一篇来自Safari Book Online的文章,是<Eclipse Distilled>下的'Chapter 6. Java Project Configuration'的内容(http://my.safaribooksonline.com/book/software-engineering-and-development/ide/0321288157/java-project-configuration/ch06lev1sec1),讲述Eclipse中的Java Project Build Path的设置.下面对其全文转贴与下.



Chapter 6. Java Project Configuration

Eclipse includes features such as Content Assist and code templates that enhance rapid development and others that accelerate your navigation and learning of unfamiliar code. Automatic compilation and building of complex projects provides additional acceleration by giving immediate feedback on code changes and project status. All of these features depend on correct configuration of the projects in your workspace.

We continue development of the product catalog and order processing application by configuring the project dependencies required to build and run that code. Part of the configuration consists of including a JAR file for the Apache log4j logging utility and a shared library of components from the Apache Axis Web Services toolkit.

This chapter does not describe configuration and use of a source code version control repository. Eclipse has excellent support for team repositories such as CVS, which is described in Chapter 13, “Team Ownership with CVS.” If you are joining an existing development team, you can skip directly to that chapter after reading this one.

In this chapter, we'll see how to

  • Configure your project's source and output folders

  • Configure dependencies between Java projects

  • Add libraries to your build path

  • Create named user libraries composed of related JAR files

  • Override workspace compiler preferences with project-specific settings

6.1. Java Build Path

Up to this point, our product catalog project had no dependencies other than the Java runtime libraries. It is now time to expand our development to include the other projects set up in Chapter 3, “Managing Your Projects.” External dependencies are also added into the mix, such as Apache log4j and Axis Web Services toolkit. Correct configuration is essential for error-free compilation, execution, and full use of Eclipse features such as Content Assist.

Edit the configuration properties for a Java project by selecting the project and choosing Project > Properties from the main workbench menu bar. The dialog shown in Figure 6-1 is opened.

 

Figure 6-1. Configuring source folders in your Java project build path.

 

 

 


The same property editor dialog may be opened using another approach. Most Eclipse resources have property pages that describe or allow you to set that resource's properties. This is true for any resource, not only for projects. The last menu item in a resource's context menu is always named Properties, and it displays a shared dialog with one or more pages that are assigned based on the resource type. Projects with a Java nature have property pages as shown in Figure 6-1. Open this dialog by right-clicking on the com.eclipsedistilled.orders project in your workbench Package Explorer view and selecting Properties.

Each Java project has its own build path that specifies all dependencies required to compile the project. Those dependencies may come from other Java projects in the workspace, from Java archive .jar files, or from folders containing .class files.

The Java Build Path properties page contains four tabs:

  • Source. The source and output folders. If you initially create a project without separate source and output folders, you can change it here. Multiple source folders can be used in one project; e.g., to separate application source code from unit tests.

  • Projects. Check-off other projects in the workspace whose output folders should be added to this build path.

  • Libraries. Location of other archive files required by this project.

  • Order and Export. Order in which projects and libraries appear in the build path and the default runtime classpath; e.g., use classes from a workspace project before using the same classes from an archive library.

6.1.1. Source and Output Folders

Each Java project is configured with a builder that automatically compiles every .java file from the source folders and saves the .class files into an output folder. Your source folder must contain subfolders for the complete package hierarchy as used by your Java source files. As described inChapter 2, “Hello Eclipse,” you can create these folder hierarchies easily using the New Java Package wizard.

All non-Java files from the source folder are copied unchanged to the corresponding folder hierarchy in the output folder. These non-Java files are usually properties files and resource files. This sometimes creates confusion when Eclipse users store other configuration or documentation files within their source folder tree and then are surprised to see them copied into the output folder. These other non-source files should be saved in regular project folders that are not configured as source folders in the configuration. You can create regular folders outside the source tree by right-clicking on a project and selecting New > Folder instead of New > Package.

Figure 6-1 shows the source folder tab in the Java project properties. This project was created with separate source and output folders named src and bin, respectively. This setup will suffice for most new projects created within Eclipse, but you can change that configuration here.

If you want to keep your unit test code within the same project as the application code, then it's a good idea to create a separate source folder, named test for example. Click the Add Folder…button on the source configuration tab and then click Create New Folder… on the resulting dialog. If you create a test folder without adding it as a configured source folder, then Java source files within it will not be compiled.

Although it's fairly common for developers to keep unit test code in the same project as the code being tested, it is preferable to create a separate project for JUnit test cases because they often need a different Java Build Path. This is especially true if you are building applications using Java runtime libraries other than the J2SE libraries. For example, if you're building a J2ME application that depends on the Mobile Information Device Profile (MIDP), you'll have to put your JUnit test cases in a separate project because JUnit requires J2ME's Foundation as a minimum class library. It's also common to use additional JUnit framework libraries when testing Web and database applications.

The most common reason for using multiple source folders is to accommodate preexisting source code that was created outside of Eclipse. Developers can be very creative when organizing their projects! A test folder is sometime embedded within the application source folder, or several logically separate source trees may be included in the same folder.

Eclipse provides other ways to split these sources into logically separate projects or source folders without changing the original structure, which might be required by other tools or Ant build files. You can add inclusion and exclusion filters on a source folder to explicitly select the files that are or are not used to build this project. For example, if documentation files are stored within the source, you could exclude **/*.html files so that they are not copied into the output folder.

There are many other possibilities for configuring preexisting code within an Eclipse project. Search for “project configuration tutorial” in the Eclipse help documentation where other detailed scenarios and suggestions are provided.

6.1.2. Project Dependencies

When we created the projects for our order management application in Chapter 3, the project dependencies were not yet specified in the configuration. These dependencies are shown as a UML package diagram in Figure 6-2. These package names are shortened versions of the fully qualified project names used in our workspace. They represent the import dependencies between top-level packages in our application, but not necessarily the dependencies of all sub-packages and external utility libraries.

 

Figure 6-2. Order processing application package dependencies.

 


Click on the Projects tab in the build path configuration, as shown in Figure 6-3. All of the projects in your current workspace are listed except for the project we are now configuring, which iscom.eclipsedistilled.orders.

 

Figure 6-3. Configuring project dependencies for com.eclipsedistilled.orders.

 


Referring to the package diagram, we see that orders depends on catalog and ubl. Configure the dependencies in your Eclipse project by selecting the checkboxes for those two projects.

The end result is that the output folders from these other two projects are included in the build path of the current project, and their classes are available while compiling classes forcom.eclipsedistilled.orders. Configuring these project references also causes their classes to be included in Quick Assist completion lists, so typing “cat” and then Ctrl+Space will now include the Catalog and CatalogItem classes in the pick list while writing the Order class.

6.1.3. Project Libraries

The Libraries tab of the Java Build Path dialog allows you to add other libraries into a project's classpath. A library is a JAR or ZIP archive containing Java class files or a project folder containing class files. An archive file may be in a project within your current workspace or elsewhere on your file system.

The library configuration for com.eclipsedistilled.orders is shown in Figure 6-4. The JRE System Library is included automatically in every Java project; it contains the standard Java APIs. We'll review two approaches for adding individual JAR libraries to this project and then create a named user library that bundles a collection of related JARs.

 

Figure 6-4. Configuring libraries for com.eclipsedistilled.orders.

 


The Libraries tab contains five buttons for specifying the location of required library files:

  • Add JARs. Select archive files located within any project from the current workspace; projects are not required to be Java projects.

  • Add External JARs. Select archive files anywhere on your file system external to the current workspace.

  • Add Variable. Use a Java classpath variable as the base path for external archives.

  • Add Library. Select from a list of user libraries that define collections of related archive files.

  • Add Class Folder. Select any folder from a project in the current workspace that contains Java.class files.

An important consideration when planning your project configuration is portability between different developer workstations in a team environment, which might include portability across operating systems such as Windows, Linux, and Macintosh. The first library option, Add JARs, is usually the most portable but not always possible or desirable when using libraries from other vendor products. Using external libraries with absolute file paths is the least portable. We'll use the first approach to add the Apache log4j library to our project (see References).

It is common practice to create a subfolder named lib within a Java project that contains other JAR files required to build the project. Right-click on your project and select New > Folder to create this folder. Download the log4j.jar binary file and copy it into your project lib folder (the file name may include a version number).

If you copy the file into your project using the operating system command line or file explorer, then your Eclipse Navigator view or Package Explorer view is updated automatically if you have automatic refresh enabled; otherwise, you must manually refresh the lib folder (see Chapter 3).

 

Tip: If you are using Eclipse on Windows, you can copy/paste or drag-and-drop files between the Windows file explorer and your Eclipse workbench folders in the same way you would between folders in the Windows Explorer. You can also cut/copy/drag between two Eclipse folders within the workbench on any operating system.

 


Now click the Add JARs… button, where you'll see a list of all projects in your workspace. Expand the project and lib folder containing log4j.jar and add it to this project's build path. It should appear as in Figure 6-4.

If you expand the log4j.jar entry in the configuration dialog, there are two optional entries about this library.

  • Source attachment. The folder or JAR file containing Java source code for classes in this library.

  • Javadoc location. The URL or external location containing a folder or ZIP file of Javadoc HTML for classes in this library.

This source attachment location is the same kind of entry we configured in Chapter 5, “Rapid Development,” to enable Javadoc hover and Content Assist for the Java runtime library. If you have source code for other libraries, such as log4j, then edit this library entry to get the same benefits when working with its classes.

You can open a Web browser with the full Javadoc HTML documentation for a library's entries by pressing Shift+F2 while the cursor is positioned on a class or method name in the editor. However, for this to work, you must configure the URL or directory where the HTML files are located.

 

Shortcut: Shift+F2: Open the full Javadoc HTML for a Java class, method, or field at the current cursor position. This command is also accessible via the menu Navigate > Open External Javadoc.

 


You can also configure the Javadoc location for the Java runtime libraries by expanding the JRE System Library in this same configuration dialog. Expand the rt.jar archive and edit the Javadoc location. This location is preset with the value http://java.sun.com/j2se/1.4.2/docs/apiwhen you install Eclipse (with version number appropriate to the JVM you used during installation). However, this will work only while you are connected to the Internet. You can change this URL to a local file path if you want to enable this feature while working offline.

This approach to project configuration is the easiest way to ensure that library locations are portable between different developer workstations and operating systems. All libraries are stored within the project folders, and locations (except for Javadoc files) are relative to the workspace home. If you zip your workspace and send it to another developer, he or she can simply unzip and open it in his or her Eclipse workbench. All project building and Content Assist will work without change.

Another way to configure library locations that also has benefits of machine and platform portability is to use classpath variables. Click the Add Variable… button in the Java Build Pathdialog, which presents a new dialog, as shown in Figure 6-5.

 

Figure 6-5. Extending a Java classpath variable in project build path.

 

 

 


In this example we'll add the standard J2EE Servlet API library to our orders.webapp project; a similar technique could be used for adding the log4j library. In Chapter 3, we reviewed the benefits of linked resource locations for gaining developer and platform portability of project files located outside of the workspace. Classpath variables are very similar to linked resource locations but require separate definitions.

Follow these steps to add a TOMCAT_HOME library location:

1.
Click the Configure Variables… button in this dialog, where you can create a new variable or change a variable location value.

2.
Add a new variable named TOMCAT_HOME with a location pointing to the root of your Tomcat application server installation, e.g., C:/jakarta-tomcat-5.0.19, and then click OK.

3.
Back in the dialog shown in Figure 6-5, select this variable and click the Extend…button, which opens the second dialog also shown in the figure.

4.
Expand the common and lib folders and then select servlet-api.jar. Click OK.

The Servlet library is now part of your project configuration. You can easily share this workspace or project with other developers who use a different path or different version of the Tomcat server. They only need to create a TOMCAT_HOME classpath variable with their location. All other aspects of this project configuration remain unchanged.

You can review and update any of your classpath variables in the Eclipse preferences categoryJava > Build Path > Classpath Variables.

6.1.4. Order and Export

After specifying project dependencies and library references, you may need to configure two other aspects of the build path. These are shown on the Order and Export tab of the Java Build Pathproperties in Figure 6-6.

  • Change the order of class path entries in situations where the same class name exists in more than one project or JAR location.

  • Choose which project or JAR entries are contributed to other projects that have this project as one of their dependencies.

 

Figure 6-6. Configuring order and export of libraries for com.eclipsedistilled.orders.

 


The same class name may exist in more than one class path entry when you have a project in your workspace that includes an updated version of some classes in one of the referenced libraries. If you want to compile and run an application using the updated version, then you must place the project above the older library version in the build path order. We assume that the library JAR file contains other classes that you need; otherwise, just remove the old library from this project's build path.

A project's build path can also include the library entries defined within one or more of its required projects. For this to work, you must explicitly export a project's libraries that are shared with its clients. However, you need to be careful when exporting a project's libraries because doing so means that those libraries can be logically thought of as part of this project. Another approach is for the client project to import the library itself, in addition to importing the required project.

Refer to the package diagram in Figure 6-2 that shows dependencies between the projects in our order processing application. The orders.webapp project depends on only the orders project, but it will likely include references to classes from catalog and ubl. When configuring the build path for orders.webapp, we can include dependencies to these other two projects, or we can export these two projects from the orders project. We take the latter approach and select the export checkboxes for catalog and ubl when configuring the orders project in Figure 6-6.

The Java Build Path order also determines the order that source folders and libraries are displayed within a project in the Package Explorer view. In most situations, the order of libraries does not affect the way a project builds or runs, so you can reorder the source folders and libraries to appear in the Package Explorer view in a way that makes it easy to find references, such as ordering libraries alphabetically.

<think>嗯,用户想了解关于完整二叉搜索树的概念和实现方法。首先,我需要明确什么是完整二叉搜索树(Complete Binary Search Tree,CBST)。根据之前学过的知识,二叉树中的完全二叉树(Complete Binary Tree)是指除了最后一层外,其他层都是满的,并且最后一层的节点都尽可能靠左排列。而二叉搜索树(Binary Search Tree,BST)的特点是每个节点的左子树所有节点的值都小于该节点的值,右子树则都大于。所以完整二叉搜索树应该是结合了这两种特性的数据结构,即同时满足完全二叉树的结构和二叉搜索树的排序性质。 接下来,我需要确认完整二叉搜索树的具体定义和特点。完全二叉树的特性使得它可以用数组高效地存储,而不需要指针,这对于内存管理是有优势的。而二叉搜索树的特性则保证了高效的查找、插入和删除操作,平均时间复杂度为O(log n)。结合起来,CBST可能在保持高效操作的同时,优化存储结构。不过,用户可能想知道如何实现这样的数据结构,或者它的具体应用场景。 然后,实现方法方面,可能需要考虑如何构建这样的树。由于是完全二叉树,节点的位置可以通过数组的索引来确定,比如对于节点i,左子节点在2i+1,右子节点在2i+2。同时,作为二叉搜索树,必须满足左子树的值小于根节点,右子树的值大于根节点。因此,构建CBST可能需要将给定的有序数组按照完全二叉树的结构进行排列。例如,对于有序数组,中序遍历完全二叉树的结构应该能得到有序序列,因此可能需要找到中间节点作为根,递归构建左子树和右子树。 另外,用户可能关心插入和删除操作在CBST中的处理。因为完全二叉树的结构要求最后一层尽可能左对齐,插入和删除可能需要调整树的结构以维持完全性,这可能会影响二叉搜索树的性质。这时候可能需要特定的算法来保持两者的平衡,或者在某些情况下,CBST可能更适合静态数据,而不是频繁变动的数据,因为维护结构可能代价较高。 还需要考虑CBST与其他类型二叉搜索树的区别,比如平衡二叉搜索树(如AVL树、红黑树)。CBST可能在存储上更紧凑,但可能在动态操作上不如平衡树高效。因此,用户可能想知道在什么情况下选择CBST更合适,比如数据静态且需要快速查找的情况下。 此外,用户的问题中提到了“实现方法”,可能需要给出具体的代码示例,比如如何从有序数组构建CBST,或者如何插入节点。例如,可以通过递归方法,每次选择中间元素作为根节点,然后递归构建左右子树,确保完全二叉树的结构。这部分需要结合算法步骤和代码实现来说明。 最后,需要验证自己的理解是否正确,是否有遗漏或错误的地方。例如,是否混淆了完全二叉树和满二叉树的概念,或者是否正确处理了二叉搜索树的排序性质与完全二叉树结构之间的冲突。比如,在构建过程中,如何确保节点位置既满足完全性,又满足左小右大的顺序,可能需要特定的构建策略,如中序遍历填充。</think>### 完整二叉搜索树(Complete Binary Search Tree)概念解析 完整二叉搜索树是**完全二叉树**和**二叉搜索树**的结合体,需同时满足以下特性: 1. **完全二叉树结构**:所有层除最后一层外均为满层,最后一层节点左对齐排列[^1] 2. **二叉搜索树性质**:任意节点的左子树值均小于该节点,右子树值均大于该节点 $$ \text{设节点索引为}i,\ \text{左子节点索引}=2i+1,\ \text{右子节点索引}=2i+2 $$ ### 核心实现步骤(以数组存储为例) 1. **数据预处理**:将输入数据排序为升序序列 2. **递归构建**: ```python def build_cbst(sorted_arr, tree, root_index=0): if not sorted_arr: return n = len(sorted_arr) mid = n // 2 # 计算中间索引 tree[root_index] = sorted_arr[mid] # 递归构建左子树(左半部分) build_cbst(sorted_arr[:mid], tree, 2*root_index+1) # 递归构建右子树(右半部分) build_cbst(sorted_arr[mid+1:], tree, 2*root_index+2) ``` 3. **空间分配**:根据完全二叉树特性预分配数组空间 $$ \text{数组长度}=2^h-1\ \text{(h为树的高度)} $$ ### 关键特性对比 | 特性 | 普通BST | 完整BST | |---------------------|---------|--------------| | 空间利用率 | 动态 | 100%(数组) | | 查找时间复杂度 | O(h) | O(log n) | | 插入/删除复杂度 | O(h) | O(n) | | 适合场景 | 动态数据| 静态数据 | ### 应用场景 1. 预先生成的静态数据集快速查询(如字典数据) 2. 需要固定存储空间的嵌入式系统 3. 需要快速序列化/反序列化的场景[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值