Reputation: 1873
This is for an assignment created in BlueJ and submitted as a zip file containing the BlueJ package.
In the package are several independent console programs. I am trying to create another "control panel" program - a gui with radio buttons to launch each program.
Here are 2 of the listener classes I have tried:
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == arraySearchButton)
{
new ArraySearch();
}//end if
else if(e.getSource() == workerDemoButton)
{
new WorkerDemo();
}//end else if
}//end actionPerformed
}//end class RadioButtonListener
private class RunButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(arraySearchButton.isSelected())
{
new ArraySearch();
}//end if
else if(workerDemoButton.isSelected())
{
new WorkerDemo();
}//end else if
}//end actionPerformed
}//end class RunButtonListener
Thanks in advance!
Upvotes: 1
Views: 202
Reputation: 4199
Assuming you are trying to launch .EXE console applications, here's some code that could help you. See below for explanation.
import java.io.*;
public class Main {
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("cmd /c dir");
Process pr = rt.exec("c:\\helloworld.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
First you need an handle on current running java application and to do so you create an Runtime Object and use Runtime.getRuntime(). You can then declare a new process and use the exec call to execute the proper application.
The bufferReader will help print the output of the generated process and print it in the java console.
Finally, pr.waitFor() will force the current thread to wait for the process pr to terminate before going further. exitVal contains the error code if any (0 means no error).
Hope this helps.
Upvotes: 1