shell command by Runtime - Java Native OS

Java examples for Native OS:Shell Command

Description

shell command by Runtime

Demo Code


import java.io.InputStream;

public class Main{
    public static void main(String[] argv) throws Exception{
        String cmd = "java2s.com";
        System.out.println(shell(cmd));
    }//from w  w w . j a va 2  s  .  c  o  m
    public static String shell(String cmd) {
        String content = "";
        try {

            Runtime shell = Runtime.getRuntime();
            Process process = shell.exec("cmd.exe  /c  " + cmd);
            InputStream is = process.getInputStream();

            content = StringUtil.valueOf(is);

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

        return content;
    }
}

Related Tutorials