我想运行命令
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: - 我的电脑已通过LAN连接到IP地址为192.168.0.8的计算机。
答案 0 :(得分:2)
使用ProcessBuilder
更好地处理外部流程。此外,除了命令名称之外,始终将参数作为单独的字符串传递。
答案 1 :(得分:1)
您需要两个线程来捕获标准输出或错误输出,如下所示:
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