Monday, 30 January 2017

Java Tutorial: Java Runtime class (exec(String[] cmdarray,String[] envp,File dir) method)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=Irfb6Fru4XQ&list=UUhwKlOVR041tngjerWxVccw

RuntimeDemo.java
import java.io.File;
import java.io.IOException;

/*
 * public Process exec(String[] cmdarray, String[] envp,
 *                              File dir) throws IOException
 * 
 * Parameters: 
 * -----------
 * 
 * cmdarray - array containing the command to call and
 * its arguments.
 * 
 * envp - array of strings, each element of which has
 * environment variable settings in the format
 * name=value, or null if the subprocess should inherit
 * the environment of the current process.
 * 
 * dir - the working directory of the subprocess, or
 * null if the subprocess should inherit the working
 * directory of the current process.
 */
public class RuntimeDemo
{

    public static void main(String[] args) throws IOException
    {

        String[] cmdArray = new String[2];

        cmdArray[0] = "notepad.exe";
        cmdArray[1] = "Hello.txt";

        System.out.println(
                "Executing notepad.exe and opening Hello.txt");

        /*
         * Returns the runtime object associated with the
         * current Java application.
         */
        Runtime runtime = Runtime.getRuntime();

        File dir = new File("D:/work");
        /*
         * Executes the specified string command in a
         * separate process with the specified environment
         * and working directory.
         * 
         * Returns: A new Process object for managing the
         * subprocess
         */
        Process process = runtime.exec(cmdArray, null, dir);

        System.out.println("is process alive = " + process.isAlive());
        System.out.println(cmdArray[1] + " should now open.");
    }

}
Output
Executing notepad.exe and opening Hello.txt
is process alive = true
Hello.txt should now open.

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RuntimeDemo_exec_cmdarray_envp_dir_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RuntimeDemo_exec_cmdarray_envp_dir_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/28b772df8f5b4b9e60fd873726243fc258cd34a2/BasicJava/RuntimeDemo_exec_cmdarray_envp_dir_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment