21

I have some classes say I have 20 classes with different objectives. Now what I want to do is that I want to make a single jar file of all of them. I want to do this because I want to use these classes as a single Jar in other projects. How can I do this?

Nosrep
  • 521
  • 7
  • 21
Jawad Amjad
  • 2,532
  • 2
  • 29
  • 59

5 Answers5

34

You can also do it using Command Line. Transfer all the classes into a folder. Say it is in the folder with absolute path C:\Java\Project1.

Go to the same folder from command prompt. And execute the following code :

jar cfv Project1.jar *

Here c will create jar, f will transfer the content into the file provided i.e. Project1.jar and v will print the process done into the console.

The Wildcard * will take all the files present in the folder. And the Jar file will be created in the same folder.

Now, you can copy this jar anywhere you want to use. :)

whitehat
  • 2,381
  • 8
  • 34
  • 47
  • 2
    `no main manifest attribute, in Project1.jar` looks like `jar cmvf META-INF/MANIFEST.MF .jar ` should work https://stackoverflow.com/a/9689877/1179925 – mrgloom Jan 10 '18 at 13:13
3

If you are using Eclipse You can do this by,

Right Click on the Package -> Export -> java -> jar file 

Package means any Folder Package or Any Group of Selected JAVA files

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
3

Just package the classes as normal!

i.e. From the command line: jar cf jar-file <input-file(s)>

More info can be found here: http://download.oracle.com/javase/tutorial/deployment/jar/basicsindex.html

vaughandroid
  • 4,315
  • 1
  • 27
  • 33
  • no. Actually i have downloaded andengine jar files from two locations both are different i want to merge them how can i do this? I want to use both of them as a single jar. – Jawad Amjad Nov 16 '11 at 13:00
  • Just extract the files from the jars (e.g. `jar xf jar-file`) then repackage them. You might want to make this clear in the original question! You might also want to state why, since there may be a better way to go about solving whatever the root problem is... – vaughandroid Nov 16 '11 at 13:08
  • ok. regrets for not making the question clear. Actually I want to start with AnDeNGINE the problem is that there are two jars available for andengine. Both are different I want to merge them and make them one jar both have many similarities.So I am unable to merge them. – Jawad Amjad Nov 16 '11 at 14:02
1

There's already a good discussion on merging multiple jars using ANT here, if you want it as part of a build process: Clean way to combine multiple jars? Preferably using Ant

Community
  • 1
  • 1
Dan
  • 364
  • 1
  • 2
0

Or you can just put the classes in one eclipse project and reference that project in the other android eclipse projects.

Right click on android project => Build Path => Configure BuildPath => 
Projects => Add

This will not work when you use ant to build your projects.

Peterdk
  • 15,625
  • 20
  • 101
  • 140