Contoh Program
package com.nursalim.javatutorial;
import java.io.IOException;
import java.io.InputStream;
/**
*
* Created by Nursalim
*/
public class CommandPromptDemo {
public static void main(String[] args) {
// menjalankan perintah "dir" di Windows
String[] command = {"cmd", "/c", "dir"};
StringBuilder cmdReturn = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec(command);
try (
InputStream inputStream = process.getInputStream()) {
int c;
while ((c = inputStream.read()) != -1) {
cmdReturn.append((char) c);
}
}
System.out.println(cmdReturn.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Hasil Eksekusi
Apabila program diatas dijalankan, maka hasilnya seperti berikut:Penutup
Sekian tutorial singkat mengenai cara Menjalankan Command Prompt di Java. Semoga bermanfaatSalam
0 comments:
Post a Comment