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?
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.
Jar files can contain only one Main-Class attribute in the manifest, which means a jar can contain only one mainClassName.
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.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With