Eclipse buildpath automatically taking all JARs of a internal directory
Asked Answered
D

10

20

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

Dispirit answered 27/1, 2009 at 14:22 Comment(0)
R
11

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.

Rhyne answered 27/1, 2009 at 14:39 Comment(1)
+1. I'm not sure why you got downvoted for this. I've done something similar and it worked, perfectly. To take it a step further, you can add your script as a builder so it's triggered automatically whenever the project is cleaned...Yila
D
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

Dispirit answered 27/1, 2009 at 15:53 Comment(2)
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 useBarrack
Link is not existing anymoreFulguration
M
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/

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 https://github.com/ctron/org.openscada.bob

Mimas answered 31/5, 2010 at 15:1 Comment(3)
Fixed the dead link to the article. This was exactly what I was looking for!Lector
thank you, I also fixed the svn link, we moved to git some time agoMimas
All links are dead.Fulguration
S
2

Use Maven2, and use the Eclipse Maven2 plugin.

Shizue answered 27/1, 2009 at 17:40 Comment(1)
Or Ivy with the IvyDE plugin.Yila
C
1

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

Sorry, Eclipse doesn't support this.

Carey answered 27/1, 2009 at 14:40 Comment(0)
M
0

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.

Martensite answered 1/4, 2009 at 2:55 Comment(0)
P
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. 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.

Print answered 12/3, 2010 at 9:59 Comment(0)
M
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.

<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

Myrtlemyrvyn answered 17/8, 2010 at 11:48 Comment(2)
The only problem with this is the path to jar files in the user library has to be absolute. This causes problems in a team (version control) environment where people have different absolute paths to their workspace. To fix this, you can create a CLASSPATH VARIABLE but then everyone has to create the variable and things get confusing, fast.Yila
This is not what was required: adding new libraries being autmatically added to the build path. User libraries require editing and explicitly adding jars, so you only shift the problem.Fulguration
A
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.

http://sweetened.googlecode.com/

Allergen answered 25/1, 2012 at 21:31 Comment(0)
L
-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.

Liaoyang answered 27/1, 2009 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.