Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jar

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?

like image 793
Jawad Amjad Avatar asked Nov 16 '11 12:11

Jawad Amjad


People also ask

Can you have multiple Java classes in one file?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.

Can a JAR have two main classes?

Jar files can contain only one Main-Class attribute in the manifest, which means a jar can contain only one mainClassName.

What is used to create JAR with all required classes?

Open the Jar File wizard In the Package Explorer select the items that you want to export. If you want to export all the classes and resources in the project just select the project. Click on the File menu and select Export. In the filter text box of the first page of the export wizard type in JAR.


1 Answers

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

like image 106
whitehat Avatar answered Oct 11 '22 20:10

whitehat