Where to put the external jars? [duplicate]
Asked Answered
K

9

54

I use Eclipse (3.4) and my class compiles without warning or errors. My project uses an external jar file.

Where do I need to put this external jar file in order not to get a java.lang.NoClassDefFoundError when using this class from another project (not in Eclipse)?

I could just extract the jar into the project folder, but that does not feel right.

Edit: this question is not about importing jars in Eclipse, but using them outside of Eclipse.

Kira answered 28/11, 2008 at 11:17 Comment(0)
L
62

If you're wanting to include a JAR file to your Eclipse project, you would generally create a 'lib' folder inside the project folder, and put the file in there. You then need to tell eclipse to include it in your class path so your code will compile and run inside eclipse.

To do that:
- Go into the properties of your project
- Select 'Java Build Path' in the left hand column
- Select the 'Libraries' tab in the centre part of the window
- Click the Add JARs button - this will give you a list of your projects in eclipse - expand your project and into the lib folder - your jar will be there.
- Select the JAR, click OK, and OK again out of the properties window.

Your code will now compile and run.

Lasonde answered 28/11, 2008 at 11:28 Comment(4)
The code does compile inside eclipse. When running it outside of eclipse (i.e. another GUI calls a function inside the jar) i get the NoClassDefFoundError.Kira
Or, you can just right-click the jar and click BuildPath->Add to Build Path.Admonitory
When you run whatever outside of eclipse, you must provide your eclipse project binaries (if not built to a jar) in the classpath of whatever you're running.Lasonde
Im totally confused where to add the jars. Inside a created lib folder? or inside war/WEB-INF/lib?Stonechat
C
25

put it in your jre/lib/ext folder

everything said about the classpath is true, but this is a consistent and sensible place for it to live.

you can find out your jre folder by looking at the JAVA_HOME environment variable on Windows.

Clarkia answered 28/11, 2008 at 11:23 Comment(4)
Interesting, but I have three JREs and three JDKs on my computer, I don't see myself duplicating also all the libraries I need.Kristankriste
you may have three (I do too), but you only use one at any time, which is defined by your JAVA_HOME. Also your 3rd party tool may have different versions for different JRE's, so you have the inverse of the duplication problem if you don't take this approach. It is what the jre/lib/ext is for.Clarkia
In short helpful enough.Dwt
The directory you mentioned (jre/lib/ext) no longer exists in recent JDK's (starting at JDK 11). What would be an appropriate location on newer JDK versions?Churchly
T
7

It doesn't matter too much where you put it, but you need to configure your other non-Eclipse project to put the external jars in its classpath - or use the extensions directory mechanism, if you must. (That's easier, but making it explicit is arguably better.)

Tupungato answered 28/11, 2008 at 11:20 Comment(2)
The non-Eclipse project is not configured nor maintained by me. In fact, i just wrote a few classes and one of these classes uses another class from a jar file.Kira
So you can't configure the other project at all in terms of classpath? Sooner or later, that's bound to bite you. Rather than work around that restriction, try to fix it.Tupungato
T
6

Simon's answer seems to be the best but a bit outdated now. Have googled this Oracle doc Installed extensions .

As of Java 6, extension JAR files may also be placed in a location that is independent of any particular JRE, so that extensions can be shared by all JREs that are installed on a system.

It says that for Windows you should place your extensions here %SystemRoot%\Sun\Java\lib\ext .

Thilde answered 24/12, 2013 at 11:12 Comment(1)
Just to confirm that this worked well in 2020 and Visual Studio Code. I added the osgi framework jar to C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext, close and opened Visual Studio Code and was able to properly import the library. As a personal note, i like this approach as it's something meant to exist by JDK itself.Pediculosis
D
1

You just need to reference it using a -classpath option, in a folder which is not included in an eclipse workspace, i.e. which does not depend on eclipse at all.

Warning, you cannoy execute your other project with java -jar if you reference your external jar with -cp option (see this question)

Decimate answered 28/11, 2008 at 11:23 Comment(0)
B
0

I place it into a new folder. This folder has to be included into the build path. It does not matter if it's in Eclipse or outside. Eclipse has a project specific build path and it passes this path to the javac. If you want to execute javac outside of Eclipse, you have to pass the build path manually.

Boice answered 28/11, 2008 at 11:21 Comment(0)
C
0

I know this isn't exactly what you are asking, but have you considered using Maven? Some of the plugins like assembly might be helpful in situations like these.

Cinereous answered 28/11, 2008 at 11:32 Comment(0)
J
0

Have a jar inside Eclipse, as James Camfield has written.

Then when you build (Ant, Maven, manually) for distribution, ensure the jar file is included with or within your application jar or war or ear or whatever file, and that any startup scripts include it on the classpath using the -classpath command line option for java, as VonC has written.

Don't worry about sticking the jars in the java extensions folder, all this will do is make you forget about it when it comes to sending your code to a third party to use, because they won't have it set up at their end.

Jedediah answered 28/11, 2008 at 11:43 Comment(0)
A
0

Create a shared lib directory and place this jar, and any other common jars files you might have there. If you are using Maven, then you already have it in the form of the local repo.

In Eclipse, create a classpath variable for this directory and then use it to reference your jar file in your projects. If you have multiple projects that meet these conditions, it will make life much easier. It also makes it easy to change the location of the root directory if you go from a local drive to a network one.

In the external app, you will also include this jar file from its shared location on the apps classpath.

Alate answered 28/11, 2008 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.