execute Shell command - Java Native OS

Java examples for Native OS:Shell Command

Description

execute Shell command

Demo Code


//package com.java2s;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] argv) throws Exception {
        String command = "java2s.com";
        System.out.println(execute(command));
    }/*from w  w  w . ja va2s.  c  om*/

    public static String execute(String command) throws IOException,
            InterruptedException {
        StringBuffer output = new StringBuffer();
        Process p = Runtime.getRuntime().exec(command);

        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }

        return output.toString();
    }
}

Related Tutorials