Example usage for java.lang Runtime getRuntime

List of usage examples for java.lang Runtime getRuntime

Introduction

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

Prototype

public static Runtime getRuntime() 

Source Link

Document

Returns the runtime object associated with the current Java application.

Usage

From source file:Main.java

public static void main(String[] args) {
    OutputStream fileOutputStream;
    try {/*from  w w w . j av  a 2  s  . c om*/
        fileOutputStream = new FileOutputStream("c:/a.txy");
        Runtime runTime = Runtime.getRuntime();
        runTime.getLocalizedOutputStream(fileOutputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Execute a command with an argument that contains a space
    String[] commands = new String[] { "grep", "hello world", "/tmp/f.txt" };

    commands = new String[] { "grep", "hello world", "c:\\f.txt" };

    Process child = Runtime.getRuntime().exec(commands);

}

From source file:Main.java

public static void main(String[] args) {
    List<Trade> trades = TradeUtil.createTrades(100);

    int cpus = Runtime.getRuntime().availableProcessors();
    long start = System.currentTimeMillis();
    trades.stream().parallel().forEach(t -> doSomething(t));
    long end = System.currentTimeMillis();

    System.out.println("Time for parallel op:" + (end - start) / 1000 + "sec on " + cpus);
}

From source file:ExecDemoWait.java

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

    // A Runtime object has methods for dealing with the OS
    Runtime r = Runtime.getRuntime();
    Process p; // Process tracks one external native process
    BufferedReader is; // reader for output of process
    String line;//from  ww  w.  j  a v  a  2  s  .c  om

    // Our argv[0] contains the program to run; remaining elements
    // of argv contain args for the target program. This is just
    // what is needed for the String[] form of exec.
    p = r.exec(argv);

    System.out.println("In Main after exec");

    // getInputStream gives an Input stream connected to
    // the process p's standard output. Just use it to make
    // a BufferedReader to readLine() what the program writes out.
    is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while ((line = is.readLine()) != null)
        System.out.println(line);

    System.out.println("In Main after EOF");
    System.out.flush();
    try {
        p.waitFor(); // wait for process to complete
    } catch (InterruptedException e) {
        System.err.println(e); // "Can'tHappen"
        return;
    }
    System.err.println("Process done, exit status was " + p.exitValue());
    return;
}

From source file:Main.java

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

    String cmd = "cmd.exe /c start ";
    // String file = "c:\\version.txt";
    // String file = "http://www.google.com";
    // String file = "c:\\";
    // String file = "mailto:author@my.com";
    String file = "mailto:";
    Runtime.getRuntime().exec(cmd + file);
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  w w .  j  av  a  2s  . co m*/

        File dir = new File("C:/");

        String[] envArray = new String[2];
        envArray[0] = "";
        envArray[1] = "";
        // create a process and execute notepad.exe and currect environment

        Runtime runTime = Runtime.getRuntime();
        Process process = runTime.exec("notepad.exe", envArray, dir);

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

}

From source file:Main.java

public static void main(String[] args) throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
    List<Particle> allTheParticles = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        allTheParticles.add(new Particle(i, allTheParticles));
    }/*from  www.ja v  a 2s .c  om*/
    while (true) {
        executor.invokeAll(allTheParticles);
        executor.invokeAll(allTheParticles);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Runtime r = Runtime.getRuntime();
    long mem1, mem2;
    Integer someints[] = new Integer[10000];

    System.out.println("Total memory is: " + r.totalMemory());

    mem1 = r.freeMemory();/*from  w w  w  . j a  v a2s.  com*/
    System.out.println("Initial free memory: " + mem1);
    r.gc();
    mem1 = r.freeMemory();
    System.out.println("Free memory after garbage collection: " + mem1);

    for (int i = 0; i < someints.length; i++)
        someints[i] = new Integer(i); // allocate integers

    mem2 = r.freeMemory();
    System.out.println("Free memory after allocation: " + mem2);
    System.out.println("Memory used by allocation: " + (mem1 - mem2));

    for (int i = 0; i < someints.length; i++)
        someints[i] = null;

    r.gc(); // request garbage collection

    mem2 = r.freeMemory();
    System.out.println("Free memory after collecting" + " discarded Integers: " + mem2);

}

From source file:Main.java

public static void main(String[] args) {
    try {/*w ww . j ava2  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:Main.java

public static void main(String[] args) {
    try {// w w w.  j a v  a  2  s. c o m
        String[] cmdArray = new String[2];

        // the program to open
        cmdArray[0] = "notepad.exe";

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

        String[] envArray = new String[2];
        envArray[0] = "";
        envArray[1] = "";

        Runtime runTime = Runtime.getRuntime();
        Process process = runTime.exec(cmdArray, envArray);

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

}