简体   繁体   中英

how to make a single jar file of multiple classes that can be used in other projects

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?

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

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

Just package the classes as normal!

ie 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM