Sunday, August 12, 2012

Creating .jar file



Create the necessary .java program files in a directory.

Employee.java

class Employee
{
 public int EmpID;
 public String EmpName;
 public Employee(int id, String name)
 {
  EmpID = id;
  EmpName = name;
 }
 public String toString()
 {
  return " Employee ID: "+EmpID + "\n Employee Name: "+EmpName;
 }

}

MyClass.java

import java.util.Scanner;
public class MyClass
{
 public static void main(String[] args)
 {
  Scanner sc=new Scanner(System.in);
  System.out.println("\n Enter Emp ID: ");
  int id = sc.nextInt();
  System.out.println("\n Enter Emp Name: ");
  String name = sc.next();
  Employee e = new Employee(id, name);
  System.out.println(e);
 }

}

Compile them using command prompt:-

javac *.java

The .class files are created.

Create manifest file to store the name of class containing main(). The line should end with new line. It can also contain version information.

manifest.txt

Main-Class: MyClass

To create jar file:-

jar cvfm MyJarFile.jar manifest.txt *.class

Jar file is created in same folder.

D:\CORE_JAVA\myjardemo>jar cvfm MyJarFile.jar manifest.txt *.class
added manifest
adding: Employee.class(in = 618) (out= 369)(deflated 40%)
adding: MyClass.class(in = 768) (out= 470)(deflated 38%)



D:\CORE_JAVA\myjardemo>dir
 Volume in drive D is Data
 Volume Serial Number is 1E89-AEA1

 Directory of D:\CORE_JAVA\myjardemo
08/12/2012  02:45 PM    <DIR>          .
08/12/2012  02:45 PM    <DIR>          ..
08/12/2012  02:45 PM               618 Employee.class
08/12/2012  02:24 PM               251 Employee.java
08/12/2012  02:22 PM                21 manifest.txt
08/12/2012  02:45 PM               768 MyClass.class
08/12/2012  02:27 PM               358 MyClass.java
08/12/2012  02:45 PM             1,439 MyJarFile.jar
               6 File(s)          3,455 bytes
               2 Dir(s)  130,187,784,192 bytes free


D:\CORE_JAVA\myjardemo>java -jar MyClass.jar

 Enter Emp ID:
420

 Enter Emp Name:
Gabbar
 Employee ID: 420
 Employee Name: Gabbar
 

No comments:

Post a Comment