How to create Jar file in Java
Java Archive (JAR) are used for packaging a large number of related files in a single archive. Usually a tool/library is shipped as Jar file which can be added to the classpath and used. Jar files are similar to compressed files like .zip or .rar with the only difference that the extension is .jar.
In this tutorial, we will see how to create Jar files in Java by using the Jar tool which is shipped with JDK and is present inside $JAVA_HOME/bin folder.
1) To create a Jar file, open command prompt and go to the directory containing the classes to be added to the Jar file.
2) Type :
jar -cf myJar.jar *.class
3) The above command will create a jar file with the name as myJar.jar and place in the current directory only.
4) For creating a Jar for packages use the command
jar -cf myJar.jar .\com\example\*.class
Anothe very easy way to create Jar files without the need of learning the commands to do so is to use Eclipse IDE where you select the packages/classes and click File->Export and export the selected classes and packages as Jar file.
After creating the jar for your application, you can simply ship it as a single file.
No comments:
Post a Comment