Kevin van der Burgt
Kevin van der Burgt

Reputation: 29

Including an jar file in a jar

I have a Java protect with the c3p0 JAR. Compiling my project in eclipse works. No errors or something. But when i try to run my application it says the following:

java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/DataSources

Do i need to give the jar file the other jarfile from c3p0? Or could i link them when launging the application?

Upvotes: 1

Views: 225

Answers (3)

Note: I've understood your problem to be when running outside Eclipse.

The standard Java classloader responsible for loading your classes does not understand jar-files inside jar-files, so you need to do something else:

  • You can merge all classes from all the jar-files you use into a single jar file. (recommended for you at this point, not general recomendation)
  • You can put jar-files inside the single jar-files and use a special classloader which understands this.
  • You can put the referenced jars "next" to the single jar file containing your class files, and have the runnable jar include the necessary MANIFEST.MF voodo needed to refer to them.

The File->Export->Runnable jar option in Eclipse can do all three based on what you choose. Pick the one best suited to how you will get the classes to the final users.

Personally I like the "jars next to the generated jar" as it is the closest to what is supported out of the box by Java, while keeping the original jar files. The simplest is most likely to merge all classes, but when you get more advanced you will find that it has some disadvantages - at this point you will most likely not encounter them.

Upvotes: 2

Cris
Cris

Reputation: 5007

It looks like you did not added c3p0 in the classpath when you run the sample.

However you could use maven and m2e plugin for eclipse and add the following

 <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
 </dependency>

beside the other dependencies you have.

after mvn eclipse: eclipse and you will have your project with a correct classpath.

Upvotes: 0

knightrider
knightrider

Reputation: 2143

Add C3P0 as external jar in eclipse

Upvotes: 0

Related Questions