1

这适用于在 BlueJ 中创建并作为包含 BlueJ 包的 zip 文件提交的作业。

包中有几个独立的控制台程序。我正在尝试创建另一个“控制面板”程序 - 一个带有单选按钮的 gui 来启动每个程序。

这是我尝试过的两个监听器类:

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

提前致谢!

4

1 回答 1

1

假设您正在尝试启动 .EXE 控制台应用程序,这里有一些可以帮助您的代码。请参阅下面的说明。

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();
            }
        }
}

首先,您需要处理当前正在运行的 java 应用程序,并为此创建一个运行时对象并使用 Runtime.getRuntime()。然后,您可以声明一个新进程并使用 exec 调用来执行正确的应用程序。

bufferReader 将帮助打印生成的进程的输出并在 java 控制台中打印。

最后,pr.waitFor() 将强制当前线程等待进程 pr 终止,然后再继续执行。exitVal 包含错误代码(如果有)(0 表示没有错误)。

希望这可以帮助。

于 2012-04-17T21:15:48.383 回答