我需要调用cowsay.exe(此程序将符号用于
绘制动物)并执行命令:Cowsay“ hello”.如何传递“ hello”作为参数?
public class cowsay {
public static void main(String[] args) throws IOException {
Process process = new ProcessBuilder("D:\\cowsay.exe","cowsay Hello").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
解决方法:
您使用java.lang.Runtime类:
public class cowsay {
public static void main(String[] args) throws IOException {
Process process =
Runtime.getRuntime().exec("cowsay hello");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
标签:java
来源: https://codeday.me/bug/20191027/1946386.html