2016-04-06 13 views
0

Belki de Linux'ta Windows'a karşı exec arasında belirgin farklar var, aşikarın ötesinde, bu yüzden bir demo yakaladım, ama aynı zamanda "merhaba dünya" nın cmdarray'u gönderildiğinde de çöküyor.exec ile çoklu komutları yankılamak için doğru kullanımı

"echo" komutlarının "komut dizisi" için doğru kullanım nedir?

kazası:

run: 
echo hello world...? cannot open notepad 
java.io.IOException: Cannot run program "echo hello world 1": error=2, No such file or directory 
BUILD SUCCESSFUL (total time: 0 seconds) 

adapte kod:

package com.tutorialspoint; 

public class RuntimeDemo { 

    public static void main(String[] args) { 
     try { 
      // create a new array of 2 strings 
      String[] cmdArray = new String[2]; 

      // first argument is the program we want to open 
      cmdArray[0] = "echo hello world 1"; 

      // second argument is a txt file we want to open with notepad 
      cmdArray[1] = "echo hello world 2"; 

      // print a message 
      System.out.println("echo hello world...? cannot open notepad"); 

      // create a process and execute cmdArray 
      Process process = Runtime.getRuntime().exec(cmdArray); 

      // print another message 
      System.out.println("example.txt should now open."); 

     } catch (Exception ex) { 
      System.out.println(ex); 
     } 
    } 
} 

ayrıca bkz: sending a cmdarray for exec to process -- hello world

+1

bakınız [zaman Runtime.exec(), sadece] (http://www.javaworld.com/article/2071275/core-java/when-runtime- Bir işlemi doğru şekilde oluşturma ve işleme konusunda birçok iyi ipucu için exec --- won-t.html). Daha sonra yoksay “exec” ifadesini kullanır ve süreci oluşturmak için bir “ProcessBuilder” kullanır. –

cevap

0

En iyi cevap: bir açıklama An

package com.tutorialspoint; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import static java.lang.System.out; 

public class RuntimeDemo { 

    public static void main(String[] args) throws InterruptedException, IOException { 
     String[] cmd = {"echo", "hello world"}; 
     Process process = Runtime.getRuntime().exec(cmd); 
     BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     String line = null; 
     while ((line = input.readLine()) != null) { 
      out.println(line); 
     } 
    } 
} 

Kesinlikle ilgi d daha iyi cevap. Ayrıca, nasıl birden çok komut gönderirsiniz ???

bakınız:

https://stackoverflow.com/a/3032498/262852

İlgili konular