2016-03-26 24 views
0

Windows'taki komut istemine erişmek ve komutları yürütmek için ProcessBuilder'ı veya başka bir şey java kitaplıklarında kullanabilir miyim? pencerelerde komut istemi ile yapmak için bir şey onların olduğunuKomut istemine (windows) java üzerinden erişme

ProcessBuilder pb = new ProcessBuilder(
     "bash", 
     "-c", 
     "cd " + System.getProperty("user.dir") + ";" + "someCommandInThatDirectory" 
    ); 

Process p = pb.start(); 

bazı dizindeki bazı komut uygulamak için: Ben mac terminali ile can biliyorum ve bu yapılan?

Bu os

String os = System.getProperty("os.name"); 
    if (os == "Mac OS X") { 
    //do the mac thing 
    } 
    else if (os == "Windows XP" /*blah blah rest of windows types*/) { 
    //do the windows one 
    } 
kontrol etmek zorunda
+0

Evet sadece yeni ProcessBuilder ("cmd/c' gibi komutunu çalıştırabilirsiniz dir c: \ ");' burada cmd komut istemidir ve pencerelerde ls alternatif komutunu çalıştırmaya çalışıyoruz. –

cevap

0
Sen Runtime.getRuntime() exec kullanabilirsiniz

.

String command = "cmd /c start cmd.exe ...your other commands"; 
try { 
    Process process = Runtime.getRuntime().exec(command); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
İlgili konular