Question

This question already has an answer here:

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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 .

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)

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top