eclipse中需要关联到第三方JAR文件时,可以直接添加JAR文件,也可以增加环境变量的形式来添加JAR文件,当然原理都是一样的,都是引用JAR文件,除了可以直接右点击工程,属性,在"Java Build Path"中添加外,其实还可以直接编辑工程目录下的 .classpath这个文件,这个文件即是记录工程中的一些环境变量及工程中文件的输出目的的,此文件结构大至如下:
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
< classpath >
< classpathentry kind = " src " path = " src/java " />
< classpathentry kind = " src " output = " target/test-classes " path = " src/test " />
< classpathentry kind = " con " path = " org.eclipse.jdt.launching.JRE_CONTAINER " />
< classpathentry kind = " var " path = " MAVEN_REPO/activation/jars/activation-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/mvc/jars/mvc-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/ehcache/jars/ehcache-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/CodeManager/jars/CodeManager-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/jetspeed/jars/jetspeed-1.4.jar " />
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
.
< classpathentry kind = " var " path = " MAVEN_REPO/dwr/jars/dwr-1.1.1.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/servletapi/jars/servletapi-2.4.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/jspapi/jars/jsp-api-2.0.jar " />
< classpathentry kind = " lib " path = " D:/workspace/repository/junit/jars/junit-3.8.1.jar " />
< classpathentry kind = " output " path = " target/classes " />
</ classpath >
< classpath >
< classpathentry kind = " src " path = " src/java " />
< classpathentry kind = " src " output = " target/test-classes " path = " src/test " />
< classpathentry kind = " con " path = " org.eclipse.jdt.launching.JRE_CONTAINER " />
< classpathentry kind = " var " path = " MAVEN_REPO/activation/jars/activation-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/mvc/jars/mvc-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/ehcache/jars/ehcache-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/CodeManager/jars/CodeManager-1.0.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/jetspeed/jars/jetspeed-1.4.jar " />
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
< classpathentry kind = " var " path = " MAVEN_REPO/dwr/jars/dwr-1.1.1.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/servletapi/jars/servletapi-2.4.jar " />
< classpathentry kind = " var " path = " MAVEN_REPO/jspapi/jars/jsp-api-2.0.jar " />
< classpathentry kind = " lib " path = " D:/workspace/repository/junit/jars/junit-3.8.1.jar " />
< classpathentry kind = " output " path = " target/classes " />
</ classpath >
其中,src是源文件输出目的的,output是类文件输出目的的,con是eclipse运行时所需的核心包,var是通过环境变量的形式增加的一些JAR包,lib是直接加入的JAR包,所以需要加入一些第三方的JAR文件时,直接编辑此文件即可,eclipse不需重启,工程即可生效.
有关说明:
有时从别的工程导入到另一个工程时,工程名旁边有一个红插,而这时可能有很多人认为是某个地方导入的不对,或有些文件没导入完全,当然也有这个可能,但大多数是因为原工程中引入了一些环境变量,而导入到本机上的eclipse时,环境变量路径发生了变化,eclipse找不到相关的JAR文年,所以工程名旁边有一个红插,而且这时当新建类,或编辑类时,类文件都不会编绎的,甚至有人认为此时是eclipse不能自动编绎了,查看自动编绎属性时又是自动编绎的,换到其他工程时又可以自动编绎,所以可能一时找不到原因,其实是.classpath文件中的一些环境变量的路径不正确导致的,去掉或指向正确的路径即可.