views:

2254

answers:

8

Hello,

i use eclipse (3.4) an my class compiles without warning nor 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)?

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

+5  A: 

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

Jon Skeet
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.
Burkhard
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.
Jon Skeet
A: 

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.

furtelwart
+1  A: 

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)

VonC
+1  A: 

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.

Simon
Interesting, but I have three JREs and three JDKs on my computer, I don't see myself duplicating also all the libraries I need.
PhiLho
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.
Simon
+7  A: 

That question doesn't make any sense...

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.

James Camfield
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.
Burkhard
Or, you can just right-click the jar and click BuildPath->Add to Build Path.
Dave Ray
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.
James Camfield
A: 

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.

javamonkey79
A: 

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.

JeeBee
+1  A: 

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.

Robin