Program to demonstrate how to create a jar file in java using runtime.exec ?
package com.hubberspot.code;
import java.io.IOException;
public class JarCommand {
public static void main(String[] args) {
// Create a string for the target location
// where finally jars will be created.
String target = "C:\\Jar";
// Create a string which will store the name
// of the jar you wish to create.
String jarName = "demo.jar";
// Create a string which will store the name of
// the folder you want to pack in the jar.
String folderToPack = "demo";
try {
// java.lang package comes with a class called as Runtime.
// getRuntime() : Returns the runtime object associated with the current
// Java application.
// Runtime class has a method called as exec() method.
// This method takes in a command which can be executed as in case
// here it is jar command
// This method returns back us with Process.
// Here in exec() method we provide with jar command as jar cf +
// passing it to
// target , jarName and folderToPack.
Runtime.getRuntime().exec("cmd /c jar cf " + target+"\\" + jarName+"
-C " +
target + "\\" + folderToPack + " .");
// The code should be written in the try catch because exec
// method throws back the IOException.
} catch (IOException e) {
e.printStackTrace();
}
}
}