java.lang.Runtime example

An example of howto execute commands in Win using java.lang.Runtime

public class Launch {
    String command = null;
 
    Launch() {
 
    }
 
    Launch(String cmd) {
        command = "cmd /c " + cmd;
    }
 
    boolean run() {
        Runtime rt = Runtime.getRuntime();
        System.out.println("exec : "+command);
        try {
            Process pr = rt.exec(command);
            pr.waitFor();
            BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String oneLine = null;
 
            while ((oneLine = br.readLine()) != null) {
                System.out.println("stdout:"+oneLine);
            }
            BufferedReader errBr = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
            while ((oneLine = errBr.readLine()) != null)
                System.out.println("stderr:"+oneLine);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

javaruntime.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0