SORU
19 NİSAN 2011, Salı


java çalışma zamanı.() getruntime komut satırı programı yürütme çıktısı almak

Çalışma zamanı java programımı komut istemi komutları çalıştırmak için kullanıyorum. Ancak çıkış komut verir nasıl alabilirim farkında değilim.

İşte benim kod.

Runtime rt = Runtime.getRuntime();

String[] commands = {"system.exe","-send",argument};

Process proc = rt.exec(commands);

Sistem yapmaya çalıştım.dışarı.print(proc); ama bu bir şey döndürmedi. Bu komutun çalıştırılması iki numaraları noktalı virgülle ayrılmış dönmelidir, nasıl yazdırmak için bir değişken bu dışarı alabilir miyim?

Teşekkürler

İşte şimdi kullanıyorum kodu:

String[] commands = {"system.exe","-get t"};

Process proc = rt.exec(commands);

InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);

String line = null;
System.out.println("<OUTPUT>");

while ( (line = br.readLine()) != null)
     System.out.println(line);

System.out.println("</OUTPUT>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: "   exitVal);

Ama benim çıkış gibi bir şey almıyorum ama kendimi komutu çalıştırdığımda gayet iyi çalışıyor.

CEVAP
19 NİSAN 2011, Salı


Burada gidilecek yol:

Runtime rt = Runtime.getRuntime();
String[] commands = {"system.exe","-get t"};
Process proc = rt.exec(commands);

BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new 
     InputStreamReader(proc.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
    System.out.println(s);
}

Daha 7 ** Daha fazla ayrıntı için Javadoc oku. ProcessBuilder kullanmak için iyi bir seçim olacaktır

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • New Scientist

    New Scientis

    27 Kasım 2006
  • Qmusic Romania

    Qmusic Roman

    8 Temmuz 2011
  • SegaAmerica

    SegaAmerica

    5 Mart 2008