且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Eclipse中将一个包含Jar文件的文件夹添加到我的类路径?

更新时间:2023-09-20 11:01:04

你不能通过IDE真的做到这一点,但是你可以做的是在IDE之外做一个程序来手动修改.classpath文件并添加jar。

You can't really do that through the IDE, but what you can do is do it outside the IDE and write a program to manually modify the .classpath file and add the jars.

例如,您的eclipse项目的.classpath文件将看起来像这样:

For example, the .classpath file for your eclipse project is going to look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
 <classpathentry kind="src" path="src/java/main"/>
 <classpathentry kind="src" path="src/java/tests"/>
 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.5.0_17"/>
 <classpathentry kind="lib" path="test_reflib/commons-codec-1.3.jar"/>
 <classpathentry kind="lib" path="test_reflib/commons-collections-3.2.1.jar"/>
 <classpathentry kind="lib" path="test_reflib/commons-fileupload-1.1.1.jar"/>
</classpath>

所以你可以做的是编写一个程序,扫描你传入的目录,并手动修改这个.classpath文件包含jars。

So what you could do is write a program which scans the directories you pass into it, and manually modifies this .classpath file to contain the jars.

个人而言,我从来没有去过这样做的麻烦,只是一般把所有需要的jar放在一个文件夹中,在项目中。或者,如果这是我需要的JBoss jar,我只包括我想要的。

Personally, I never go to the trouble of doing this and just typically put all the jars I need in one folder and include them in the project. Or, if it's something like JBoss jars I need, I just include the ones I want.