Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: Adding all the jars from a folder in java classpath

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.

like image 758
Rafi Kamal Avatar asked Sep 04 '12 15:09

Rafi Kamal


People also ask

How do I include all JARs in a folder classpath?

In general, to include all of the JARs in a given directory, you can use the wildcard * (not *. jar ). The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.

How do I find the classpath of a jar file?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.

How do I select multiple JARs in Eclipse?

Hold the shift key down while clicking on additional jars to add within the folder. This will allow you to select and add more than one at a time.


3 Answers

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.

like image 145
Aaron Digulla Avatar answered Oct 11 '22 11:10

Aaron Digulla


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

like image 36
Grim Avatar answered Oct 11 '22 09:10

Grim


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

like image 42
Devanshi Mittal Avatar answered Oct 11 '22 09:10

Devanshi Mittal