29

Is there a way to include all the jar files within a directory in the classpath using Eclipse?

It can be done by using command line by the following command:

java -cp "lib/*" my.package.Main

But how can it be done from eclipse?

EDIT: I edited the .classpath entry in my project floder as:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/> 
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="lib" path="F:/Program Files/Java/log4j/apache-log4j-2.0-alpha2-bin/*"/>
    <classpathentry kind="output" path="classes"/>
</classpath>

Here F:/Program Files/Java/log4j/apache-log4j-2.0-alpha2-bin/ is the folder where the jar files are. But it didn't work.

UPDATE: It seems that there is no direct way to do this. And may be the easiest solution will be creating a user library.

Rafi Kamal
  • 4,522
  • 8
  • 36
  • 50
  • Easy to do in IntelliJ: Add the directory as a library source. I'm sure there's something equivalent in Eclipse. – duffymo Sep 04 '12 at 15:32

4 Answers4

29

Instead of using a folder outside of your project, copy all necessary JARs into a lib/ folder inside of your project.

Now you can select all JARs in that folder, right click -> Build Path -> Add To Build Path.

Alternatively, you can define a "User Library" which contains all the JARs and add that single item to your project's build path.

But the best solution IMO, is to use Maven for this. Get the M2E Plugin for Eclipse and convert your project to a Maven project. Now you can use "Add Dependency" to add millions of other project (including everything they need) to your project. Maven will download and manage the dependencies for you.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thanks for suggesting Maven. I'll give a try. – Rafi Kamal Sep 04 '12 at 15:50
  • I have dependencies which do not have a Maven Repository - what would be your suggestions in that case? – ProfVersaggi Nov 06 '14 at 17:00
  • 1
    @ProfVersaggi: Install them in your local repo or set up a proxy in your network. Don't try to manage dependencies yourself unless you know *exactly* what you're doing. I know that beginners feel this is wasted effort but there is a minefield of bugs waiting for you if you stray off the beaten path. – Aaron Digulla Nov 10 '14 at 10:17
  • Maven is commonly used and does handle dependency trees nicely --but it comes with some downsides, such as a slow edit-compile-run cycle. I'd think twice about whether its worthwhile for a project before using it. – Daniel Winterstein Oct 01 '19 at 21:34
  • @DanielWinterstein The IDE will fix the "slow edit-compile-run cycle" and using Maven will make you (mostly) independent of any IDE. – Aaron Digulla Oct 07 '19 at 07:24
4

Not without a Plug-in. I do not know why this feature is not a basic part of eclipse.

https://github.com/benjchristensen/SimpleDirectoryContainer_EclipsePlugin

Grim
  • 1,938
  • 10
  • 56
  • 123
1

In eclipse (windows) right click on the libs folder(contains all the jar files) that you want to add in classpath as well -> Build Path-> Configure Build Path -> click on classpath -> add class folder -> select the folder of java files ...so this way you can multiple jar files in classpath

-1

Right Click Project -> Properties.. -> Java Build Path -> Libraries -> Add Class Folder...

Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40