.jar, the wrapped or archived Java application to be used or run on any system with required java run time (JRE). It should have main method, the entry point to start the execution of the application. You can create the jar file with the help of inbuilt Java Archive Tool which is provided in JDK.
Let’s have one Hello World Jar. Consider following Java program which just print the ‘Hello Jar!’ in console.
public class Test { public static void main(String args[]) { System.out.println("Hello Jar!"); } }
To create jar of this simple program(application) we have to use following basic command.
jar cf jarFile inputFiles
Here cf has following meaning,
- c : means create jar
- f : means provide output in file instead of standard ouput
Consider that we have placed the file at D:/ImproveJava
. So, for our simple Test.java
it goes following way.
D:\ImproveJava>javac Test.java D:\ImproveJava>java Test Hello Jar! D:\ImproveJava>jar cf Hello.jar Test.class D:\ImproveJava>java -cp Hello.jar Test Hello Jar!
Here -cp stands for class path, you now have Hello.jar
. You can run this file in any system which has JRE in it.
This is just a simple example, but you can create and run jar with different options and commands. Moreover, some smart IDEs gives options to directly export the project as jar file. We will discuss about these options in next upcoming posts.