Creating a JAR file in Java

This section provides you to the creating a jar file
through the java source code by using the jar tool command which is provided by
the JDK (Java Development Kit). Here, you can learn how to use the jar command,
which is used in the dos prompt, in the java source code to perform same
operations.
Following program is given for best illustration about
using the dos used command in the java source code to perform the appropriate
tasks. This program has been used the jar command "jar cf jar-file-name
directory-name" in the source code to create a jar file for the given
directory. You can also specify the file name(s) to collect it into the jar file
format.
Code Description:
Runtime:
Runtime is the class of java.lang.*; package of Java. This class
helps application to interface with the environment in which the java
application is running.
Runtime.getRuntime():
This method gets the current runtime environment.
Runtime.exec(String[] command):
This method helps you to run the command of that environment in which your
java application is running.
Here is the code of the program:
import java.io.*;
public class CreateJar{
public static void main(String[] args) throws IOException{
if(args.length <= 0){
System.out.println("Please enter the command.");
System.exit(0);
}
else{
Runtime.getRuntime().exec(args[0] + " " + args[1] + " " + args[2] + " " + args[3]);
}
}
}
|
Download this example.

|