Sunday, June 30, 2013

Creating executable Jar files

    To make an executable jar file, one must have a .java file, let's create one sample:

Create a folder of some name and create the java file in there, so as to avoid confusion

HelloWorld.java


public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World");

}
}

save it with.java extension, now we have HelloWorld.java

now open cmd from Run > cmd, hit enter

compile the java class, so to create a .class file like this:


Now we have a class file created in the drive, now create a manifest file with name like manifest.txt, this is a text file.

write in this txt (notepad) file, 

Main-Class: HelloWorld

In this file, HelloWorld is the name of Class that contains the main method, also hit enter in notepad after writing class name like this:



Now we have these files:

1. .java file
2. .class file
3. manifest file

now open the cmd and run this command in the folder that contains the java file, .class file, manifest file

jar cfm some_name.jar manifest.txt HelloWorld.class

you can use any name for the jar file that you are gonna create as long as there is a .jar after the name, when you hit enter it must not show any error like this:



 it must create a .jar file in the folder like this:


now if you double click this file it will not show you anything

right click on this file and open this file with winrar or some compression software, you may see something like this:


 Open the test.jar with winrar and it must show these files, the .class file and META-INF folder

Open META-INF folder and you'll see


open this file with notepad, like this:


and you'll see this:


After making sure these things are right, go to cmd and execute the following command:

java -jar test.jar

and this will print "Hello World", which is present in the java file


This is what we used in println in the java file, now the jar is ready and is executable.


Comments

blog comments powered by Disqus