Wednesday, 1 January 2014

Create Executable .jar files (Java Archive)

Jar stands for Java Archive.

It is java binary file available in "jdk/bin" folder.
It is used to compress or group Files and Folders.
Hence ''.jar " file is compressed file that is created by using "jar" command that contains java library files.


Difference between "zip" and "jar" extension files

zip is a platform dependent compressed file it works only in windows,where as jar is platform independent compressed file that can work in any OS,because it is created by using JAVA.



How can we create jar file using jar command?

Step:1 Open commandprompt.
Step:2 Type jar press Enter you will find usage of jar command with various options.
            The import options are
             -c create archive.
             -v shows the files adding to this archive. 
             -f  specify file name
             -x extract the files from jar.
             -e  (for 'entrypoint') creates or overrides the manifest's Main-Class attribute.
It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp

Step:3 Type jar -cfe app.jar MyApp MyApp.class

MyApp.class file only is added to jar file from current dierctory.
jar -cfe app.jar MyApp *.class
All .class files are added to this jar file.

How an executable jar file is made?

The jar that allows us to execute a main method class available in it is called executable jar file.
In every jar,zip file we have META-INF folder created by jar command.
To develop executable jar file we must configure main method class name in its "manifest.mf" file it is available in META-INF folder created automatically in every jar file.
File is properties files contains (key,value) pair.
All keys are predefined by SUN those keys value must be assigned by developer.
The required key and its associated value both must be placed by developer, like how we are writing main method and its logic in a class.
The key for configuring the class name is "Main-Class".


For Example:

Main-Class:Test

How can we execute jar file?

java -jar Test.jar

Sample of Creating jar file and executing it:

Step:1 Create a folder Test
E:/Test

Step:2 Create Test.java file and save it in folder
Test.java

public class Test{
  public static void main(String[] args){
     Addition.add(10,20) ;
}
}

Addition.java

public class Addition{
 public static void add(int a,int b){
   System.out.println("a+b:"+(a+b));
  }

}

Step:3 Compile java files
D:\Test>javac *.java

Step:4 Creating jar file
jar -cfe Test.jar Test *.class
Test.jar -->jar name
Test-->Class name which contains main method
*.class-->To include all .class files.
Step:5 Execution
java -jar Test.jar

output:
a+b:30

Hard to remember all commands so we create batch file on it if we double click it run.

Open normal text file
Type java -jar Test.jar
Save it in D:\Test folder with name Test.bat

Open Command prompt

E:\Test>Test.bat

Output:
E:\Test>java -jar Test.jar
a+b:30

If we want to make independent of Directories set Classpath

classpath=E:\Test\Test.jar;

Now we can access from any directory.






   

No comments: