Wednesday, 10 April 2013

Running Another Application Independently from Java Application


Hi Friends,

I would like to share you some notes regarding ProcessBuilder, which helps in execution of seperate Java application.

We can run the Java application not only from console or bat/sh file, but even from another Java application also.

We can make use of Runtime class and ProcessBuilder classes for running the java application.

Now i would like to share you the process of executing java application individually from another java application using ProcessBuilder class.

Below is the snippet for Running java application as independent process.

package test;
import java.io.File;
import java.io.IOException;
/**
 * @author naveen.k
 */
public class ProcessBuilderDemo {

    public int runProcess(){
        try {
            String javaHome = System.getProperty("java.home");
            String javaBin = javaHome +
                    File.separator + "bin" +
                    File.separator + "java";
            ProcessBuilder probuilder = new ProcessBuilder(javaBin, "-cp", "d:\\", "TestProcess", "Naveen", "Naresh");
            Process process = probuilder.start();
            System.out.println("Process started successfully");
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    public static void main(String[] args) {
            new ProcessBuilderDemo().runProcess();
    }
}

With the above execution of code, you can observe a new Java.exe task in Task Manager, with executes independently.

Below is the given TestProcess.java file which is provide as argument for above ProcessBuilder constructor:


import java.io.FileOutputStream;
import java.io.PrintWriter;
/**
 * @author naveen.k
 */
class TestProcess {

    public static void main(String[] args) {
        try {
            int i = 0;
            for (i = 0; i < args.length; i++) {
                System.out.println("Hello " + args[i] + "!");
            }
            FileOutputStream fos = new FileOutputStream("D:\\Test\\output.txt");
            PrintWriter bos = new PrintWriter(fos);
            int count = 0;
            while (count++ <= 100) {
                bos.write(" ******* " + count + " ******* ");
                bos.write("\n");
                for (i = 0; i < args.length; i++) {
                    bos.write("Hello " + args[i] + "!");
                    bos.write("\n");
                }
            }
            bos.flush();
            fos.flush();
            bos.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


After executing the ProcessBuilderDemo class, even if you closes/terminates the ProcessBuilderDemo class, the TestProcess execution will be continued.

Let's assume, we are calling the TestProcess class from the Web-application. Even, if we shut-down the server also the TestProcess execution will be continued.

For more details regarding ProcessBuilder you can check this space.

1 comment:

  1. Very useful information that you have shared and it is very useful to me. Thanks for sharing the information with us.
    kindle mobi formatting services

    ReplyDelete