tags:

views:

1365

answers:

9

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 ?

I am using Eclipse 3.4

A: 

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.

workmad3
A: 

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

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.

Yoni Roit
A: 

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

Sorry, Eclipse doesn't support this.

Kevin
+4  A: 

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

exactly the right answer - classpath containers are pretty easy to write, but you need to deliver a plugin for the rest of your team to use
Scott Stanchfield
+1  A: 

Use Maven2, and use the Eclipse Maven2 plugin.

Ben Hardy
A: 

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

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.)

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.

A: 

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. Delete directory "lib" under WebContent/WEB-INF/
  3. Recreate that directory, but create it as link to the directory in your main project where your jars are located
  4. Check Web App Libraries option on Order and Export tab in Java Build Path settings of your project
  5. Add this newly created project as a dependency to your main project

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.

calavera.info
A: 

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/news/dx/31.05.2010154336JREJ4U.htm

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

The plugin is licensed unter LGPL V3 and you can find the source code under http://pubsvn.inavare.net/openscada/modules/bob/trunk/

Mauli
A: 

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.

<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

Murali