`

Eclipse classpath file format

    博客分类:
  • JAVA
 
阅读更多

经常遇到eclipse中jar依赖问题,每次都需要看eclipse生成的classpath文件,为了有个权威的说明,故找来了这个文件的格式说明(真不好找>_^),为了确保内容以后不丢失,我先放一份在blog里。

 

 

转载自:http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-classpath/section2.html

 

 

 

Simplify Eclipse classpaths using classpath containers

 

Introduction to classpath containers

Classpath containers are an effective way to organize project resources by grouping them under one logical classpath entry. Whether you realize it or not, you may have used a classpath container. The most recognized classpath container among Java developers is the JRE System Library. Every Java project has a JRE System Library in the classpath. Other notable classpath containers are the JUnit and Plug-in Dependencies containers included in the base Eclipse project, as well as the Web App Libraries container, which is part of the dynamic Web project type in the Web Tools Project (WTP). Before we start implementing our own classpath container, let's start by reviewing the different types of classpath entries.

Kinds of classpath entries

Every Java project includes a .classpath file that defines the classpath of the project. This file is generally not edited by hand but created and modified by the JDT plug-in as the user changes the Java build path properties of a project. Each entry in the classpath has a kind attribute and a path attribute, along with various other optional attributes depending on the kind. The order the entries appear in the file from top to bottom determines their order in the project's classpath. Before continuing, it is a good exercise to create a Java project and experiment with that project by changing the Java build path properties, creating entries and modifying the initial default entries to arrive at the .classpath file shown in Listing 1.


Listing 1. Sample .classpath file showing all entry kinds

                    
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="lib" path="lib/derby.jar" sourcepath="lib/derby-src.jar"/>
    <classpathentry kind="var" path="ECLIPSE_HOME/startup.jar"/>
    <classpathentry combineaccessrules="false" kind="src" path="/bar"/>    
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="output" path="bin"/>
</classpath>


Listing 1 shows six classpath entries marked by five kinds. In the underlying Eclipse Java model, each of these is represented by an org.eclipse.jdt.core.IClasspathEntry type, which categorizes the entry-by-entry kind and content kind. A content kind is defined for most entry kinds and is either source (org.eclipse.jdt.core.IPackageFragmentRoot.K_SOURCE) or binary (IPackageFragmentRoot.K_BINARY). In Eclipse V3.2, the IClasspathEntry interface defines five constants to refer to the classpath entry kind, but these are not the same five marked in Listing 1. In fact, one shown in this file — namely output — is not defined as a constant in IClasspathEntry, and another — src — is shown twice in Listing 1 because the two are represented as separate constants in IClasspathEntry. This makes it a little confusing, but let's ignore that for now and start from the top of the file.

Project source folders

The first entry in Listing 1 has kind="src" with path="src". This indicates that the project has a source folder located in a directory relative to the project root called src. This will have several implications for the project — most notably, it tells the Java builder that it needs to compile the source located in the src directory. This entry kind is represented by the constantIClasspathEntry.CPE_SOURCE and has a content kind of IPackageFragmentRoot.K_BINARY. When Java projects are created, they inherit default source and output folders from workspace preferences (in Eclipse see Window > Preferences > Java > Build Path). In a fresh Eclipse installation, the source and output folders will be set to the root project folder. The first src entry in Listing 1 has a path="src" because the workspace default source folder was changed to src.

Binary libraries

The second entry in Listing 1 has kind="lib" with path="lib/derby.jar". This indicates that there is a binary classpath entry located at lib/derby.jar relative to the project root. lib entries refer to a set of class files with the path attribute specifying a directory of .class files or an archive file that includes .class files. This entry also includes the optional sourcepath attribute, which refers to the location of the source attachment. The lib kind entry is represented by the entry kind constantIClasspathEntry.CPE_LIBRARY and has a content kind of IPackageFragmentRoot.K_BINARY.

Classpath variable extensions

The third entry in Listing 1 has kind="var" with path="ECLIPSE_HOME/startup.jar". In this entry, ECLIPSE_HOME is a variable. This built-in variable provided by Eclipse refers to the Eclipse installation directory. Custom variables can also be defined by the user for a workspace. Paths to classpath files are always relative to the project directory or absolute. So it's good practice to use variables for referencing files outside the project directory to avoid absolute paths, which will make the project difficult to share across different environments. See Window > Preferences > Java > Build Path > Classpath Variables for a list of the variables defined in your workspace. After a variable is defined, it can be used in the build path settings by extending it to refer to a file that is located relative to the variable.

In this case, it was extended by adding the Eclipse startup.jar. var entries are similar to lib entries, except that the first element in the path is evaluated before using it in the classpath. Another difference between var and lib entries is that lib entries can reference an archive or a folder of classes, whereas var entries can only reference an archive. The var kind is a binary type of entry represented by the constant IClasspathEntry.CPE_VARIABLE and the content kind is undefined. So, you should never usegetContentKind() when referring to an IClasspathEntry with kind="var". It is interesting to note, however, that one can only add binary variable extensions to the classpath, and to make it confusing, getContentKind() always returnsIPackageFragmentRoot.K_SOURCE for variable entries.

Prerequisite projects

The fourth entry in Listing 1 has kind="src" with path="/bar". This entry references the source of another project in the workspace (notice that the path starts with /, which refers to the workspace root directory). This will, in effect, add the configured output folder for the /bar project to the compilation classpath, as well as any classpath entries exported from the project. While the kind attribute is equal to "src", as it is with IClasspathEntry.CPE_SOURCE above, this entry is represented by a different entrykind constant, IClasspathEntry.CPE_PROJECT, and it has a content kind of IPackageFragmentRoot.K_SOURCE. The only difference in the .classpath entry is that the path attribute is relative to the workspace, instead of the project.

Classpath containers

The fifth entry in Listing 1 has kind="con" with path="org.eclipse.jdt.launching.JRE_CONTAINER". This type of entry is the subject of this tutorial. The kind constant for this entry is IClasspathEntry.CPE_CONTAINER, and the content kind is undefined. A container entry is a logical reference to an implementation of org.eclipse.jdt.core.IClasspathContainer. The implementation aggregates a set of concrete entries, either of type IClasspathEntry.CPE_LIBRARY or IClasspathEntry.CPE_PROJECT.

Summary of classpath entries

A summary of the classpath entry types is provided below. The example entries are taken from the .classpath file shown inListing 1.


Table 1. Summary classification of classpath entries in Listing 1

Example entry Icon Entry kind Content kind <classpathentry kind="src" path="src"/> <classpathentry kind="lib" path="lib/derby.jar" sourcepath="lib/derby-src.jar"/> <classpathentry kind="var" path="ECLIPSE_HOME/startup.jar"/> <classpathentry combineaccessrules="false" kind="src" path="/bar"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
Project Source Folder CPE_SOURCE K_SOURCE
Library CPE_LIBRARY K_BINARY
Variable CPE_VARIABLE undefined
Project Prerequisite CPE_PROJECT K_SOURCE
Container CPE_CONTAINER undefined


More information

For a detailed explanation of the different classpath entries, read the Javadoc for org.eclipse.jdt.core.IClasspathEntry, and for a good lesson on setting the build path programmatically with these different kinds, read the Eclipse help topic "Setting the Java Build Path."

What are classpath containers?

In this tutorial, the term classpath container refers to an implementation of org.eclipse.jdt.core.IClasspathContainer. Classpath containers are logical references to a set of classpath resources. Their entry sets can be defined per project by theIClasspathContainer implementation. Since the entry set is defined by the IClasspathContainer, it can be dynamic or static. As an example of the static case, a container could return a fixed set of JARs that are located in a well-known directory (e.g., the JRE System Library). Or, in a more dynamic case, a container might return a set of runtime JARs that changes depending on server deployment.

Why use classpath containers?

There are many reasons why one might want to use a classpath container. Here are the more important ones:

Reducing clutter
Instead of having several individual JARs under one project, you can group them into a container that can be expanded to see the individual JARs. This conserves screen real estate in the Java Package Explorer view.

And the number of entries needed in the .classpath file is reduced from several to one.
Portability
Since the paths for container entries are logical names, there is no information about the file system included. This makes the classpath entry portable to different machines and, thus, makes it easy to share projects through source control management (SCM) systems, such as CVS or SVN. Variable entries also have this benefit.
Easier management
If a classpath container page is defined for the container type, the user can edit the properties of the container through the UI.
Dynamicity
This is probably the most important benefit, since it cannot be achieved through any of the other classpath entry types. Since the container entries are retrieved at the time they are needed, they can be very dynamic. No information about the container entries persists in the workspace or project metadata. The entries are retrieved when they are needed for operations like compiling and running. This provides a means for redefining the project's classpath dependencies based on practically any factor. In a simple case, a container could add all the JARs in a project directory to the classpath. In fact, this is the function of the Web App Libraries classpath container provided by the Web Tools Project (WTP) for a Web project's WEB-INF/lib folder.
分享到:
评论

相关推荐

    JUnit Ant Eclipse, JUnitReport报告

    标题 "JUnit Ant Eclipse, JUnitReport报告" 涉及到的是在软件开发过程中,使用JUnit进行单元测试,结合Ant构建工具生成测试报告,并在Eclipse集成开发环境中进行管理和查看的过程。这里我们将详细探讨这三个核心...

    eclipse连接haoop

    - 将 Hadoop 的 JAR 包添加到 Eclipse 的 Classpath 中。 - 在 Eclipse 的项目上右键,选择 “Properties” &gt; “Java Build Path” &gt; “Libraries”。 - 点击 “Add External JARs...”,选择 Hadoop 目录下的...

    eclipse+gradle解决android 65k 方法问题

    [2015-06-12 12:26:57 - Iphone] Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536 ``` 这表明应用中的方法数已经超过了 Dalvik 虚拟机能处理的最大值。 #### 三...

    Fortify-SCA-扫描工具指导手册.pdf

    sourceanalyzer -b &lt;build-id&gt; -cp &lt;classpath&gt; &lt;file-list&gt; ●附注参数:-Xmx;- encoding-jdk;- appserver- appserver- veron -appserver-home Table 1: File specifiers File specifier Description darna盈e All ...

    在ubuntu13.10环境中配置hadoop.docx

    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH ``` - 保存并关闭文件后,执行`source ~/.bashrc`使更改生效。 #### 安装Eclipse 1. **下载Eclipse:** - 访问...

    java统计源码行数

    System.err.format("Error reading file: %s%n", e.getMessage()); } System.out.println("File contains " + lineCount + " lines."); } } ``` 上述代码创建了一个`BufferedReader`实例,然后使用`readLine()`...

    解析Excel表格(Apache POI)

    为了在Eclipse中运行这个示例,确保项目结构包含`.classpath`和`.project`文件,这表明这是一个Eclipse项目。`lib`目录可能包含了Apache POI的JAR文件,而`.settings`目录包含项目的配置信息。`bin`目录通常是编译后...

    tif_demo.zip

    在本项目中,"tif_demo.zip" 是一个包含Spring Boot应用程序的压缩文件,该程序旨在演示如何在前端使用tiff.js库来展示由后端返回的TIFF(Tagged Image File Format)图像文件。TIFF是一种广泛用于存储高质量图像的...

    Hibernate学习笔记

    为了在Eclipse中正确配置Hibernate,首先需要设置Classpath Variables来指向Hibernate安装路径下的核心库文件。具体步骤如下: 1. 打开Eclipse并选择`Window -&gt; Preferences`打开首选项对话框。 2. 在左侧菜单中...

    Lucene与javaWeb整合测试

    `.classpath`和`.project`文件是Eclipse项目配置,`.settings`是Eclipse工作空间设置,`lib`目录可能包含项目依赖的JAR文件,而`bin`目录通常包含编译后的类文件。 整合Lucene与JavaWeb需要理解Lucene的核心概念和...

    jxl(java_excel_jxl_oracle_imp_exp_使用全攻略).txt

    通常,在IDE(如Eclipse或IntelliJ IDEA)中,可通过项目构建路径或Maven依赖管理来完成这一操作。 #### 创建Excel文件 创建一个Excel文件涉及使用`WritableWorkbook`对象,它是负责创建新工作簿的核心类。下面的...

    java生成二维码

    - **.classpath**:记录了项目所需的类库路径,包括ZXing库的引用。 - **lib**:可能包含了ZXing库的JAR文件,作为项目的依赖。 5. **注意事项** - 二维码的内容有限制,过长的数据可能会导致生成失败或读取困难...

    fckedit编辑器

    FormatOutput=true/false 当输出内容时是否自动格式化代码 FormatSource=true/false 在切换到代码视图时是否自动格式化代码 FullPage=true/false 是否允许编辑整个HTML文件,还是仅允许编辑BODY间的内容 ...

    JSP配置

    在开发环境中,通常通过IDE(如Eclipse、IntelliJ IDEA)自动管理类路径。在生产环境中,需要确保所有的依赖库被正确地添加到服务器的类路径中。在Tomcat中,可以在`$CATALINA_HOME/lib`目录下放置JAR文件,或者在`...

    Hibernate实践 DB操作

    &lt;taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="app.classpath"/&gt; &lt;!-- 执行生成映射文件 --&gt; ,@author,@todo"&gt; &lt;fileset dir="src/java"&gt; **/po...

    安卓ADT项目及升级AS(Android Studio)全攻略

    &lt;format pattern="yyyyMMddhhmm" property="pktime" unit="hour"/&gt; &lt;!-- 创建apk存放目录 --&gt; ${apk.dir}"/&gt; &lt;!-- 将第三方jar包添加到classpath变量中 --&gt; ${toString:project.all.jars.path}"/&gt; ${tz....

    使用Maven assembly打多个自定义包及War包简介

    `.classpath`和`.project`文件是Eclipse的工作空间配置文件,它们包含了项目的类路径和项目设置信息,主要用于IDE的项目管理。`.settings`目录包含了Maven项目的用户特定配置,这些文件通常是IDE自动生成的,对于...

Global site tag (gtag.js) - Google Analytics