这是我第一次在这些论坛上提问。我正在开发一个程序,它将ping特定主机并显示ping值。 Ping处理程序单独工作,但是当我尝试连接gui时它无法正常工作。这是代码: 此方法成功搜索ping:
public static void runSystemCommand(String command) {
int i=0;
while (i<2){
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
Thread.sleep(9000);
} catch (Exception e) {
e.printStackTrace();
}
i++;
}
第一个问题:当我想用开始按钮在gui上使用它时。首先,我尝试使用静态布尔值false。下面的代码将无法识别boolean已更改为true,因此它仅显示Display ping :.
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getActionCommand().equals("Display Ping")) {
System.out.println("Display ping: ");
GUI_COMPLETE = true;
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new gui().setVisible(true);
if (GUI_COMPLETE) {
pingmain p= new pingmain();
String ip = "google.com";
p.runSystemCommand(ip);
}
}
}
);
第二个问题:如果我将静态布尔值更改为true,那么gui main中的代码片段将起作用,但是在按下开始按钮后它会给我这个错误,所以我无法通过gui ping一个主机:
java.io.IOException: Cannot run program "google.com": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at pingviewer.pingmain.runSystemCommand(pingmain.java:25)
at pingviewer.gui$4.run(gui.java:140)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
at java.lang.ProcessImpl.start(ProcessImpl.java:137)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 19 more
我很抱歉长度,但我无法用言语解释。任何帮助将不胜感激。