Skip to main content

Executing executable Jar file from Java Program


Sample code to execute jar file from Java Program.

Pre-requisite
1. Need to have executable jar file.
2. Copy the code in your workspace and Run the code.

 package com.test.reusable.enterprise.service;  
 import java.io.BufferedReader;  
 import java.io.File;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.InputStreamReader;  
 import org.apache.commons.lang.exception.ExceptionUtils;  
 public class ExecuteJarFile {  
      public static void main(String[] args) {  
           try {  
                //Method1 to Execute Jar file From Java Program  
                     ProcessBuilder pb = new ProcessBuilder("java", "-jar", "HelloWorld.jar"); // command to execute jar file with fileName.jar  
                  pb.redirectErrorStream(true);  
                  pb.directory(new File("C:/www/")); // Directory path where your jar file is placed.   
                  Process p = pb.start();  
                  InputStream is = p.getInputStream();  
                  BufferedReader br = new BufferedReader(new InputStreamReader(is));  
                  System.out.println("===== Method 1 output =====");  
                  for (String line = br.readLine(); line != null; line = br.readLine()) {  
                      System.out.println( line ); // Or just ignore it  
                  }  
                  p.waitFor();       
                //Method2 to Execute Jar file From Java Program  
                  Runtime re = Runtime.getRuntime();  
                  BufferedReader output = null;             
                  try{   
                   Process cmd = re.exec("java -jar C:/www/HelloWorld.jar"); // command to execute jar file with complete path  
                   output = new BufferedReader(new InputStreamReader(cmd.getInputStream()));  
                  } catch (IOException ioe){  
                   ioe.printStackTrace();  
                  }  
                  String resultOutput = output.readLine();  
                  System.out.println("===== Method 2 output =====");  
                  System.out.println("resultOutput :: "+resultOutput);  
           } catch (Exception e2) {  
                String fullStackTrace = ExceptionUtils.getFullStackTrace(e2);  
                System.out.println("Caught Exception :: "+fullStackTrace);  
           }  
      }  
 }  
=========== Output ===========
I hope that above given example gives you a better understanding of executing jar file from java program.

Comments

Popular posts from this blog

Creating Pdf in multiple Languages in Java

Creating Pdf in multiple Languages in Java This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator. To use this sample code download itext.jar To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font (Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);) You can download the source code from here import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf....

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } ...

Collections in Java

Collections in Java Introduction to Collections Framework The Collections Framework provides a well defined set of interfaces and classes for storing and manipulating the groups of data into a single unit. Introduction to Collections Framework Collections Framework: The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating the groups of data into a single unit. The collections framework is a unified architecture which is used to represent and manipulate collections. The framework allows the collections to get manipulated independently, additionally it reduces the programming efforts and increases performance. It includes implementation of interfaces and algorithms. Basically it is a unified architecture that consists the following collections: Interfaces: These are the abstract data types that represent collections. With the help of interfaces we manipulate collections independently. A hierarchy is generally formed ...