I saw the "Programmer" Issue 3 I call other programs in the Java expert clinic, and I put it on a program, as shown in:
Import java.lang. *;
Public class runprog {
Public static void main (String [] args) {
Try {
Runtime RT = runtime.getRuntime ();
rt.exec ("notepad");
} catch (exception e) {}
}
}
Compile operations under the command, call the Notepad app without any problems.
However, in the application of graphical users, they cannot be compiled, and the code examples are as follows:
Void JButton1_ActionPerformed (ActionEvent E) {
/ / The next is the answer code
Try {
Runtime RT = runtime.getRuntime ();
rt.exec ("notepad");
} catch (exception e) {
}
// On the answer code
}
For the above code, it just explains the basic method of calling other programs, but this code cannot be compiled at all, and the compilation error in jbuilder is as follows:
"Frame2.java": Error #: 469: Variable E Is Already Defined in Method JButton1_ActionPerformed (Java.awt.Event.ActionEvent) At line 50, Column 18
Seeing this compilation error may be a button event definition error. It is actually a thread security level. It is not allowed to use additional processes or threads directly, and the components in Swing are inherited from the AWT. So don't allow direct use. The solution only uses a new thread. The code is shown:
Void JButton1_ActionPerformed (ActionEvent E) {
// must be use a new thread.
Thread T = New Thread (New Runnable () {
Public void run () {
Try {
Runtime RT = runtime (). GetRuntime ();
rt.exec ("notepad");
} catch (ioexception e) {
System.err.Println ("IO Error:" E);
}
}
});
T.Start ();
}
But this code still cannot be compiled, the error prompts are as follows:
"Frame1.java": Error #: 300: Method runtime () Not found in anonymous class of method jbutton1_actionperformed (java.awt.event.ActionEvent) At line 74, Column 22.
See this code, I don't think of runtime (), or there is no package where Runtime is located. But in fact, every application of Java has its own Runtime, so it is not allowed to declare and use another one. In fact, many articles are also present. Here you must use Process to enable another process using Runtime. The code example is, for example:
Void JButton1_ActionPerformed (ActionEvent E) {
// must be use a new thread.
Thread T = New Thread (New Runnable () {public void run () {
Try {
// String [] arrcommand = {"javaw", "-jar", "d: /unicom/salary/salary.jar"};
// process p = runtime.getRuntime (). EXEC (arrcommand);
Process p = runtime.getime (). EXEC ("notepad");
p.WaitFor ();
System.out.println ("Return Code:" P.exitValue ());
} catch (ioexception e) {
System.err.Println ("IO Error:" E);
} catch (interruptedException E1) {
System.err.Println ("Exception:" E1.GetMessage ());
}
}
});
T.Start ();
}
After running, click JButton1 to call the Notepad app in Windows. Here, the new thread uses the Runnable interface, which is a common skill. In addition, you must also capture both anomalies for IOEXCEPTION and InterruptedException. For complex programs that call with parameters, use the string array to replace simple strings, I commented on the code above.