user2511713
user2511713

Reputation: 555

Creating jar issue

This question may be theoretical but I could not find any proper solution.

Suppose I am making a module which uses 3 jar file(hibernate,log4j,jackson).

Now I want to compile my module and create a jar such that my module can be used by any other module and that module should not require the three jars(hibernate,log4j,jackson) to again.

i.e my module jar file should not have any dependencies.

I am using eclipse.I am able to create a jar(project->export->jar) but it does not include the jars in it Guide me how can I do that.

Is apache ant of any use here?

Upvotes: 1

Views: 93

Answers (2)

Ankur Lathi
Ankur Lathi

Reputation: 7836

Eclipse's Runnable Jar Wizard

Eclipse's Runnable Jar Wizard (File → Export… → Java → Runnable Jar File) allows developers to create executable jars from an existing run configuration:

enter image description here

The wizard includes 3 options for handling dependencies:

  1. Extract required libraries into generated jar: unarchives library dependencies and repackages them into your executable jar. This option has the advantage of simplicity and does not require a custom class loader. However repackaging library jars can cause other problems and does not preserve the signatures of signed jars. This option may also violate the license terms of the libraries you are using.

  2. Package required libraries into generated jar: creates a "fat jar" with a custom class loader. The resultant jar contains the application's classes and resources library jars required to launch the application a small custom class loader that knows how to find jar libraries inside another jar archive

  3. Copy required libraries…: creates the application archive and copies any required library dependencies to the destination folder.

I think the second option suits your present purpose.

Upvotes: 1

NickJ
NickJ

Reputation: 9579

You will need to include all classes from your dependencies into your jar file. Since a jar file is merely a zip file, you can use any archive manager such as Winzip to explore them, then copy the contents of the jars you depend on into your own jar, taking care to keep the directory structure intact.

That way you have everything in one jar.

Upvotes: 0

Related Questions