Differences

This shows you the differences between two versions of the page.

Link to this comparison view

javaruntime [2014/10/26 01:52] (current)
Line 1: Line 1:
 +==== java.lang.Runtime example ====
 +
 +An example of howto execute commands in Win using java.lang.Runtime
 +<code lang=java>​
 +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;
 +    }
 +}
 +</​code>​
 +
 +----
 +
 +  * [[javainfo|Back to Java]]
  
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