Launch Java process programmatically in Java

  sonic0002        2016-04-27 04:03:30       8,570        0    

In some cases while working on some automation testcase, developers would like to launch Java process programmatically so that the tests can be ran without manual intervention. Java provides such methods to achieve this.

ProcessBuilder can be used to build a Java process which can be launched when everything is ready. Basically it can take a list of parameters which are similar to the command line options/arguments. 

For example, if you want to launch a Java process, you can do following.

public final class JavaProcess {
	private String output = "";
	private String javaHome = "";
	
	public JavaProcess(String javaHome) {
		this.javaHome = javaHome;
	}
	
    public int exec(Class klass) throws IOException, InterruptedException {    	
        String javaBin = javaHome +
                File.separator + "bin" +
                File.separator + "java";
        String classpath = System.getProperty("java.class.path");
        String className = klass.getCanonicalName();

        ProcessBuilder builder = new ProcessBuilder(
                javaBin, "-cp", classpath, className);
        builder.redirectErrorStream();
        
        Process process = builder.start();
        
        // Read and process the command output
        readOutput(process.getInputStream());
        
        process.waitFor();
        
        return process.exitValue();
    }
    
    /**
     * Read and process the command output, doesn't include error.
     * 
     * @param input
     */
    private void readOutput(final InputStream input) {
    	new Thread(new Runnable() {
    		public void run() {
    			StringBuilder sb = new StringBuilder();
    			int data;
    			try {
	    			while((data = input.read()) != -1) {
	    				sb.append((char)data);
	    			}
	    			
	    			output = sb.toString();
    			} catch (Exception ex) {
    				ex.printStackTrace();
    			}
    		}
    	}).start();
    }

	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}
}

Then you just create a JavaProcess instance with the Java home specified and the class you want to run.

JavaProcess process = new JavaProcess(javaHome);
process.exec(ClassToRun.class);
String output = process.getOutput();

This is quite similar to the command line command :

java -cp classpath ClassToRun

When ProcessBuilder instance is created, you can call a set of methods to  set up its environment like redirecting errors etc. After all settings are done, you just need to call ProcessBuilder.start() to create a process and then waiting for its execution completion. During this period, you can start to read its the command output. After the command is executed, you can also get the status code by calling exitValue() to determine whether the command is executed successfully or not.

This is extremely convenient and useful while creating automation jobs such as automation test case. 

JAVA  COMMAND LINE  PROCESSBUILDER 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Hello world on production