[WikiDyd] [TitleIndex] [WordIndex

Jak uruchomić z Javy zewnętrzny program?

   1 public static void main(String[] args) {
   2         try {
   3             String line;
   4             String cmd = "cmd /c dir"; // MS Windows
   5             // String cmd = "ls -l";      // Unix/Linux
   6             Process p = Runtime.getRuntime().exec( cmd );
   7             BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
   8             BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
   9             while ((line = bri.readLine()) != null) {
  10                 System.out.println(line);
  11             }
  12             bri.close();
  13             while ((line = bre.readLine()) != null) {
  14                 System.out.println(line);
  15             }
  16             bre.close();
  17             p.waitFor();
  18             System.out.println("Done.");
  19         } catch (Exception err) {
  20             err.printStackTrace();
  21         }
  22     }
  23 }

2015-09-23 06:44