0

I have java program with interface created using Java Swing. The interface contains a JButton. I want to add Action to that button so that it should execute another Java program which I specify.

How to do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

Try this:

  yourButton.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
          try {
              Process proc = Runtime.getRuntime().exec("java -jar A.jar");
          } catch (IOException ex) {

          }
      }
  });
DoubleMa
  • 368
  • 1
  • 8
  • See [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. **Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process.** – Andrew Thompson May 09 '15 at 03:53
  • 2
    Huh.. I voted down before I even saw `} catch (IOException ex) { }` ..can I have another turn? That should be `} catch (IOException ex) { ex.printStackTrace(); }` – Andrew Thompson May 09 '15 at 03:55