Example usage for java.lang Runtime exec

List of usage examples for java.lang Runtime exec

Introduction

In this page you can find the example usage for java.lang Runtime exec.

Prototype

public Process exec(String cmdarray[]) throws IOException 

Source Link

Document

Executes the specified command and arguments in a separate process.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/*from www  .ja v  a 2s . c o m*/
        // create a process and execute notepad.exe
        Runtime runTime = Runtime.getRuntime();
        Process process = runTime.exec("notepad.exe");

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

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

    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(args);
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;//ww w. j a v  a2 s .co  m

    System.out.printf("Output of running %s is:", Arrays.toString(args));

    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }

}

From source file:ExecDemo.java

public static void main(String args[]) {
    Runtime r = Runtime.getRuntime();
    Process p = null;/*ww w.  ja  v  a2 s  . c o m*/

    try {
        p = r.exec("notepad");
    } catch (Exception e) {
        System.out.println("Error executing notepad.");
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {//w  w  w  .  ja  v  a  2  s  .  c o m
        String[] cmdArray = new String[2];
        // first argument is the program to open
        cmdArray[0] = "notepad.exe";

        // data.txt is the file to open with notepad
        cmdArray[1] = "data.txt";

        // create a process and execute cmdArray

        Runtime runTime = Runtime.getRuntime();

        Process process = runTime.exec(cmdArray);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:ExecDemoFini.java

public static void main(String args[]) {
    Runtime r = Runtime.getRuntime();
    Process p = null;/*from w  w w . ja va2 s .  c o  m*/

    try {
        p = r.exec("notepad");
        p.waitFor();
    } catch (Exception e) {
        System.out.println("Error executing notepad.");
    }
    System.out.println("Notepad returned " + p.exitValue());
}

From source file:org.openmrs.ModuleStory.java

public static void main(String args[]) throws IOException, InterruptedException {
    if (!skipDatabaseSetupPage()) {
        String databaseUserName = System.getProperty("database_user_name", "root");
        String databaseRootPassword = System.getProperty("database_root_password", "password");
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("curl http://localhost:8080/openmrs/auto_run_openmrs?local=en&remember=true"
                + "&database_user_name=" + databaseUserName + "&database_root_password="
                + databaseRootPassword);
        log.debug("Waiting 10 minutes for OpenMRS installation to complete!!");
        Thread.sleep(1000 * 60 * 10); //Waiting 10 minutes for installation to complete
    }/*from  ww  w .ja va  2 s  . c  om*/
}

From source file:ExecShellArgs.java

public static void main(String[] args) throws IOException {
    Runtime r = Runtime.getRuntime();
    String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done" };
    Process p = r.exec(nargs);
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;//ww w  .  ja  va 2 s  .co m
    while ((line = is.readLine()) != null)
        System.out.println(line);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Runtime r = Runtime.getRuntime();
    String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done" };
    Process p = r.exec(nargs);
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;//from  w w w .  j a  va  2 s  .c  o m
    while ((line = is.readLine()) != null)
        System.out.println(line);
}

From source file:MainClass.java

public static void main(String args[]) {
    Runtime r = Runtime.getRuntime();
    Process p = null;//from   w  ww  .ja va  2s  . c  om
    String cmd[] = { "notepad", "/java/src/java/lang/Runtime.java" };
    try {
        p = r.exec(cmd);
    } catch (Exception e) {
        System.out.println("error executing " + cmd[0]);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Runtime r = Runtime.getRuntime();
    Process p = null;/*from   w  w w. j  a v  a 2s  .c  om*/
    String cmd[] = { "notepad", "/java/src/java/lang/Runtime.java" };
    try {
        p = r.exec(cmd);
        p.waitFor();
    } catch (Exception e) {
        System.out.println("error executing " + cmd[0]);
    }
    System.out.println(cmd[0] + " returned " + p.exitValue());
}