Javaè¿è¡å½ä»¤è¡å¹¶è·åè¿åå¼ï¼ä¸é¢ä»¥ç®åçJavaæ§è¡pingå½ä»¤ï¼ping 127.0.0.1 -t
ï¼ä¸ºä¾ï¼ä»£ç å¦ä¸ï¼
你好,谢谢
能再详细一下吗?
Runtime rt= Runtime.getRuntime();
String []cmd= {"c:\\","mallet import-dir --number 10"};//mallet 是一个运行程序
Process pr = rt.exec(cmd);
我这样运行,有错误,网上我也找不到多条语句怎么运行?
我不需要读取输出,只要能像在command line里面运行一样即可。
你把 response 里面的 run 方法改成你要的, request 里面的 run 方法是发送命令。样例中我向 request 中发送了 dir 命令,然后在 response 中得到了输出。
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
public static void main(String[] args) throws IOException {
Process shell = Runtime.getRuntime().exec("cmd.exe");
final InputStream input = shell.getInputStream();
final OutputStream output = shell.getOutputStream();
Thread request = new Thread() {
public void run() {
try {
output.write("dir".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
};
request.start();
Thread response = new Thread() {
public void run() {
int c = -1;
byte[] buf = new byte[512];
try {
while ((c = input.read(buf)) != -1) {
System.out.print(new String(buf, 0, c));
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
response.start();
}
}