我想运行命令
ipmsg.exe /MSG 192.168.0.8 "文本信息"
通过一个java程序。有谁能够帮我?
我正在尝试以下几行。但它不工作..
Runtime run_t = Runtime.getRuntime();
Process notify = run_t.exec("cmd /c ipmsg.exe /MSG 192.168.0.8 Hello");
PS:-我的电脑通过局域网连接到IP地址为192.168.0.8的电脑。
使用 aProcessBuilder
可以更好地处理外部流程。此外,除了命令名称之外,始终将参数作为单独的字符串传递。
您需要两个线程来捕获标准输出或错误输出,如下所示:
package demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecDemo {
public static void main(String[] args) throws Exception {
final Process p = Runtime.getRuntime().exec("nslookup google.com");
Thread stdout = new Thread() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = br.readLine())!=null) {
System.out.println(line);
}
br.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
Thread stderr = new Thread() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
try {
while ((line = br.readLine())!=null) {
System.out.println(line);
}
br.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
//
stdout.start();
stderr.start();
//
stdout.join();
stderr.join();
//
p.waitFor();
}
}
输出(在 Mac OS X 中):
Server: 192.168.6.1
Address: 192.168.6.1#53
Non-authoritative answer:
Name: google.com
Address: 74.125.31.113
Name: google.com
Address: 74.125.31.138
Name: google.com
Address: 74.125.31.139
Name: google.com
Address: 74.125.31.100
Name: google.com
Address: 74.125.31.101
Name: google.com
Address: 74.125.31.102