How to create jar file | easy way of .jar from .java files
One fine day I was just skimming through files and thinking of how we can create jar files that contains the package and that jar is inserted into big java project and used the classes from that files there, You might have think what’s great deal just searched and find so many ways nothing seems to be perfect and contain all the details
Steps
- lets have folder that contain all java files required for which we want one jar file, Lets have a look on image below
Here point to note is we have package mypkg which inmost cases you also have for larger project for which you are making jar
2. Now lets compile java program that has package so we will keep .class generated from it into that package / folder using
javac -d mypkg One.java
3. now let create a manifest.mf file like following which contains the name of your class which contains main method that is your entry point make sure here to give class name with package name shown in image below, Keep in mind to write it like this after: there is space and one extra new line at end of file
Main-Class: One.java
4. Now most important part is to create jar file using cmd this command ship from jdk no need to add any other library
jar -cvmf manifest.mf one.jar mypkg/*.class
5. finally test the jar that you have created is correct or not
java -jar one.jar
If everything is correct it will start running program from entry point of main method psvm()😅
If not able to run program just read through article again and carefully watch on screenshots . I know you will definitely able to make.
If you like this article please leave a clap 👏.
Enjoy my other tech articles you will learn something from it.😎