Creating jar file from class file

Description

Executable jar file can be created for the set of class files. Following steps demonstrates the creation of Sorting.jar file for the class file Sorting.class.

  • Filename: Sorting.java

import java.util.*;
class Sorting {
public static void main(String args[]) {
//Sorting Integer in natural order
int n[] = {40,20,29,65,74};
Arrays.sort(n);
System.out.println("Sorted list of Integer");
for(int i:n) {
System.out.println(i);
}
//Sorting String in natural order
String s[] = {"dog","cat","animal","bird","dove"};
Arrays.sort(s);
System.out.println("Sorted list of String");
for(String i:s) {
System.out.println(i);
}

}
}

  • Commands to be executed

To create jar file:

jar – cvfe Sorting.jar Sorting Sorting.class

To execute jar file:

java -jar Sorting.jar