Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to create a Jar file in java using runtime.exec ?.

Program to demonstrate how to create a jar file in java using runtime.exec ?

package com.hubberspot.code;

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) {

© 2021 Learn Java by Examples Template by Hubberspot