Eclipse buildpath自动获取内部目录的所有JAR

时间:2023-01-15 21:14:44

How do I configure my project buildpath to have a set of .jar files located in the same directory automatically included in the buildpath ? Meaning that adding a new .jar file to this directory (and refreshing the project) updates the buildpath ? Rem : I am not working in a Webapp but in a standalone Java app. I know that it is possible in a Dynamic Web Project to have all the .jars located in WEB-INF/lib to be included in the build path. Is it possible to do kind of the same include but in standalone app ?

如何配置项目构建路径以使一组位于同一目录中的.jar文件自动包含在构建路径中?这意味着将新的.jar文件添加到此目录(并刷新项目)会更新构建路径? Rem:我不是在Web应用程序中工作,而是在独立的Java应用程序中工作。我知道在Dynamic Web Project中可以将WEB-INF / lib中的所有.jars包含在构建路径中。是否可以在独立应用程序中执行相同的包含?

I am using Eclipse 3.4

我正在使用Eclipse 3.4

10 个解决方案

#1


11  

AFAIK, there's no normal way to do this.

AFAIK,没有正常的方法可以做到这一点。

If you really want, there's a little hack. Eclipse .classpath file is a very simple XML. You can write a script or ant task that goes over a directory, updates .classpath xml and refreshes the project.

如果你真的想要,那就有点黑客了。 Eclipse .classpath文件是一个非常简单的XML。您可以编写遍历目录的脚本或ant任务,更新.classpath xml并刷新项目。

#2


9  

Using a homemade "ClassPath Container" solves the problem but needs you to build an Eclipse-plugin : http://www.ibm.com/developerworks/edu/os-dw-os-eclipse-classpath.html

使用自制的“ClassPath容器”解决了这个问题,但需要您构建一个Eclipse插件:http://www.ibm.com/developerworks/edu/os-dw-os-eclipse-classpath.html

#3


5  

My colleague implemented a classpath container which recursivly looks for jars in a given directory within the workspace, have a look at http://openscada.org/2010/05/31/adding-a-directory-as-class-path-to-eclipse/

我的同事实现了一个类路径容器,它在工作区内的给定目录中递归查找jar,看看http://openscada.org/2010/05/31/adding-a-directory-as-class-path-to -蚀/

The update site can be found at http://repo.openscada.org/p2/bob/R

更新站点位于http://repo.openscada.org/p2/bob/R

The plugin is licensed unter LGPL V3 and you can find the source code under https://github.com/ctron/org.openscada.bob

该插件是LGPL V3的许可证,您可以在https://github.com/ctron/org.openscada.bob下找到源代码。

#4


2  

Use Maven2, and use the Eclipse Maven2 plugin.

使用Maven2,并使用Eclipse Maven2插件。

#5


1  

Meaning that adding a new .jar file to this directory (and refreshing the project) updates the buildpath

这意味着将新的.jar文件添加到此目录(并刷新项目)会更新构建路径

Sorry, Eclipse doesn't support this.

对不起,Eclipse不支持这个。

#6


0  

On a windows machine you could do what uzhin said with a one liner on the command line.

在Windows机器上,您可以使用命令行上的一个内衬来执行uzhin所说的内容。

Supposing you had set up a variable in Eclipse called AXIS2_HOME that points to c:\javalibs\axis2-1.4.1, this example uses the "for" command to iterate all of the .jar files in the c:\javalibs\axis\lib directory and writes the .classpath nodes a file called addto.classpath. (Note: The carat character ^ escapes the < and > on the command line so they don't do what they usually do.)

假设您在Eclipse中设置了一个名为AXIS2_HOME的变量,该变量指向c:\ javalibs \ axis2-1.4.1,此示例使用“for”命令迭代c:\ javalibs \ axis中的所有.jar文件lib目录并在.classpath节点中写入一个名为addto.classpath的文件。 (注意:克拉字符^在命令行中转义 <和> ,因此它们不会执行它们通常执行的操作。)

for %i in (c:\javalibs\axis2-1.4.1\lib\*.jar) do @echo     ^<classpathentry kind="var" path="AXIS2_HOME/lib/%~ni%~xi"/^>  >> addto.classpath

You end up with something like this in the file...

你最终在文件中得到这样的东西......

<classpathentry kind="var" path="AXIS2_HOME/lib/activation-1.1.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/annogen-0.1.0.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/axiom-api-1.2.7.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/axiom-dom-1.2.7.jar"/>
...

So then you can copy those lines and paste them in the obvious place in the actual .classpath file.

那么你可以复制这些行并将它们粘贴到实际的.classpath文件中的显而易见的位置。

#7


0  

I'm using workaround which I developed after reading similar question. I'm posting it here, because that question has a little bit different scope and it might not be obvious how to use suggestions written there.

我正在使用我在阅读类似问题后开发的解决方法。我在这里发布它,因为这个问题的范围有点不同,如何使用那里写的建议可能并不明显。

  1. Create new dynamic web project (default settings is ok so it is fast)
  2. 创建新的动态Web项目(默认设置正常,因此速度很快)
  3. Delete directory "lib" under WebContent/WEB-INF/
  4. 删除WebContent / WEB-INF /下的目录“lib”
  5. Recreate that directory, but create it as link to the directory in your main project where your jars are located
  6. 重新创建该目录,但将其创建为主项目中您的jar所在目录的链接
  7. Check Web App Libraries option on Order and Export tab in Java Build Path settings of your project
  8. 在项目的Java Build Path设置中的Order and Export选项卡上检查Web App Libraries选项
  9. Add this newly created project as a dependency to your main project
  10. 将此新创建的项目添加为主项目的依赖项

It's irritating to do this for every lib directory in your project but it's better than adding jars manually every time you do svn update (from your luckier colleagues using Idea). It's a shame that Eclipse don't have this feature.

对项目中的每个lib目录执行此操作很烦人,但这比每次执行svn更新时手动添加jar更好(来自使用Idea的幸运同事)。遗憾的是Eclipse没有这个功能。

#8


0  

You can use a User Library in Eclipse can be a useful way to organize a set of jar files. If you have a set of jar files that you use in several projects, you can create a User Library to reference the set of jar files. As a result, all you would need to include in your project's build path is the User Library rather than all the individual jar files.

您可以在Eclipse中使用用户库,这是组织一组jar文件的有用方法。如果您有一组在多个项目中使用的jar文件,则可以创建一个用户库来引用一组jar文件。因此,您需要在项目的构建路径中包含的只是用户库而不是所有单独的jar文件。

<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/User Library name"/>

http://www.avajava.com/tutorials/lessons/what-is-a-user-library-and-how-do-i-use-it.html;jsessionid=ACD75E0A035BDC3AC656B481A7830FB6

http://www.avajava.com/tutorials/lessons/what-is-a-user-library-and-how-do-i-use-it.html;jsessionid=ACD75E0A035BDC3AC656B481A7830FB6

#9


0  

I built a tool which generates the .classpath (and .project), but it requires you to specify the list of jars in your ant build. It could probably be extended to read a list of jar files.

我构建了一个生成.classpath(和.project)的工具,但是它要求你在ant build中指定jar列表。它可能会扩展为读取jar文件列表。

http://sweetened.googlecode.com/

http://sweetened.googlecode.com/

#10


-2  

Have you tried using a wildcard in the classpath? So you would use something like:

您是否尝试在类路径中使用通配符?所以你会使用类似的东西:

javac MyApp.java -cp /some/path/to/libraries/*.jar

I've not used java for a while so not sure if the above is possible, but it's what I would expect to be the syntax.

我已经有一段时间没用java了,所以不确定上面是否可行,但这就是我期望的语法。

#1


11  

AFAIK, there's no normal way to do this.

AFAIK,没有正常的方法可以做到这一点。

If you really want, there's a little hack. Eclipse .classpath file is a very simple XML. You can write a script or ant task that goes over a directory, updates .classpath xml and refreshes the project.

如果你真的想要,那就有点黑客了。 Eclipse .classpath文件是一个非常简单的XML。您可以编写遍历目录的脚本或ant任务,更新.classpath xml并刷新项目。

#2


9  

Using a homemade "ClassPath Container" solves the problem but needs you to build an Eclipse-plugin : http://www.ibm.com/developerworks/edu/os-dw-os-eclipse-classpath.html

使用自制的“ClassPath容器”解决了这个问题,但需要您构建一个Eclipse插件:http://www.ibm.com/developerworks/edu/os-dw-os-eclipse-classpath.html

#3


5  

My colleague implemented a classpath container which recursivly looks for jars in a given directory within the workspace, have a look at http://openscada.org/2010/05/31/adding-a-directory-as-class-path-to-eclipse/

我的同事实现了一个类路径容器,它在工作区内的给定目录中递归查找jar,看看http://openscada.org/2010/05/31/adding-a-directory-as-class-path-to -蚀/

The update site can be found at http://repo.openscada.org/p2/bob/R

更新站点位于http://repo.openscada.org/p2/bob/R

The plugin is licensed unter LGPL V3 and you can find the source code under https://github.com/ctron/org.openscada.bob

该插件是LGPL V3的许可证,您可以在https://github.com/ctron/org.openscada.bob下找到源代码。

#4


2  

Use Maven2, and use the Eclipse Maven2 plugin.

使用Maven2,并使用Eclipse Maven2插件。

#5


1  

Meaning that adding a new .jar file to this directory (and refreshing the project) updates the buildpath

这意味着将新的.jar文件添加到此目录(并刷新项目)会更新构建路径

Sorry, Eclipse doesn't support this.

对不起,Eclipse不支持这个。

#6


0  

On a windows machine you could do what uzhin said with a one liner on the command line.

在Windows机器上,您可以使用命令行上的一个内衬来执行uzhin所说的内容。

Supposing you had set up a variable in Eclipse called AXIS2_HOME that points to c:\javalibs\axis2-1.4.1, this example uses the "for" command to iterate all of the .jar files in the c:\javalibs\axis\lib directory and writes the .classpath nodes a file called addto.classpath. (Note: The carat character ^ escapes the < and > on the command line so they don't do what they usually do.)

假设您在Eclipse中设置了一个名为AXIS2_HOME的变量,该变量指向c:\ javalibs \ axis2-1.4.1,此示例使用“for”命令迭代c:\ javalibs \ axis中的所有.jar文件lib目录并在.classpath节点中写入一个名为addto.classpath的文件。 (注意:克拉字符^在命令行中转义 <和> ,因此它们不会执行它们通常执行的操作。)

for %i in (c:\javalibs\axis2-1.4.1\lib\*.jar) do @echo     ^<classpathentry kind="var" path="AXIS2_HOME/lib/%~ni%~xi"/^>  >> addto.classpath

You end up with something like this in the file...

你最终在文件中得到这样的东西......

<classpathentry kind="var" path="AXIS2_HOME/lib/activation-1.1.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/annogen-0.1.0.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/axiom-api-1.2.7.jar"/>
<classpathentry kind="var" path="AXIS2_HOME/lib/axiom-dom-1.2.7.jar"/>
...

So then you can copy those lines and paste them in the obvious place in the actual .classpath file.

那么你可以复制这些行并将它们粘贴到实际的.classpath文件中的显而易见的位置。

#7


0  

I'm using workaround which I developed after reading similar question. I'm posting it here, because that question has a little bit different scope and it might not be obvious how to use suggestions written there.

我正在使用我在阅读类似问题后开发的解决方法。我在这里发布它,因为这个问题的范围有点不同,如何使用那里写的建议可能并不明显。

  1. Create new dynamic web project (default settings is ok so it is fast)
  2. 创建新的动态Web项目(默认设置正常,因此速度很快)
  3. Delete directory "lib" under WebContent/WEB-INF/
  4. 删除WebContent / WEB-INF /下的目录“lib”
  5. Recreate that directory, but create it as link to the directory in your main project where your jars are located
  6. 重新创建该目录,但将其创建为主项目中您的jar所在目录的链接
  7. Check Web App Libraries option on Order and Export tab in Java Build Path settings of your project
  8. 在项目的Java Build Path设置中的Order and Export选项卡上检查Web App Libraries选项
  9. Add this newly created project as a dependency to your main project
  10. 将此新创建的项目添加为主项目的依赖项

It's irritating to do this for every lib directory in your project but it's better than adding jars manually every time you do svn update (from your luckier colleagues using Idea). It's a shame that Eclipse don't have this feature.

对项目中的每个lib目录执行此操作很烦人,但这比每次执行svn更新时手动添加jar更好(来自使用Idea的幸运同事)。遗憾的是Eclipse没有这个功能。

#8


0  

You can use a User Library in Eclipse can be a useful way to organize a set of jar files. If you have a set of jar files that you use in several projects, you can create a User Library to reference the set of jar files. As a result, all you would need to include in your project's build path is the User Library rather than all the individual jar files.

您可以在Eclipse中使用用户库,这是组织一组jar文件的有用方法。如果您有一组在多个项目中使用的jar文件,则可以创建一个用户库来引用一组jar文件。因此,您需要在项目的构建路径中包含的只是用户库而不是所有单独的jar文件。

<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/User Library name"/>

http://www.avajava.com/tutorials/lessons/what-is-a-user-library-and-how-do-i-use-it.html;jsessionid=ACD75E0A035BDC3AC656B481A7830FB6

http://www.avajava.com/tutorials/lessons/what-is-a-user-library-and-how-do-i-use-it.html;jsessionid=ACD75E0A035BDC3AC656B481A7830FB6

#9


0  

I built a tool which generates the .classpath (and .project), but it requires you to specify the list of jars in your ant build. It could probably be extended to read a list of jar files.

我构建了一个生成.classpath(和.project)的工具,但是它要求你在ant build中指定jar列表。它可能会扩展为读取jar文件列表。

http://sweetened.googlecode.com/

http://sweetened.googlecode.com/

#10


-2  

Have you tried using a wildcard in the classpath? So you would use something like:

您是否尝试在类路径中使用通配符?所以你会使用类似的东西:

javac MyApp.java -cp /some/path/to/libraries/*.jar

I've not used java for a while so not sure if the above is possible, but it's what I would expect to be the syntax.

我已经有一段时间没用java了,所以不确定上面是否可行,但这就是我期望的语法。