0

let's say I have a JButton called "Play", and when I click this, it should launch C:/play.exe. How do I manage to do this? I'd love to see an example.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Paludan
  • 622
  • 1
  • 5
  • 21
  • If you are looking to run `CMD` comands then Read [Run cmd commands through java](http://stackoverflow.com/questions/15464111/run-cmd-commands-through-java) – Smit Dec 12 '13 at 20:08
  • this forum is about [SSCCE](http://sscce.org/), your effort, lets say can be accepted on another forums, otherwise read official Oracle tutorial, there is everything with excelent details – mKorbel Dec 12 '13 at 20:10
  • Look up examples for ProcessBuilder and read the docs... – MadProgrammer Dec 12 '13 at 20:10
  • @mKorbel What do you mean about my effort? I asked a precise question, isn't that what his website it about? I've coded the entire program, and now I need to make my Jbutton execute the downloaded .exe file that it downloaded. But I'm not sure how to do this, that's why I asked. – Paludan Dec 12 '13 at 20:11
  • @user2966573 mr korbel means that you have to show up some code what you have tried to do. – nachokk Dec 12 '13 at 20:12
  • something that exactly to identify how did you so far ..., this is question for google, can returns then same sugestions – mKorbel Dec 12 '13 at 20:14

2 Answers2

0

Take a look at the javadoc for ProcessBuilder which contains an example of how to create a process on the underlying system.

From there it's a simple matter of hooking it up to the button's ActionEvent

Jason Braucht
  • 2,358
  • 19
  • 31
0

Have a look at the Runtime.getRuntime().exec() method. See this example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class Main extends JFrame {
   public Main() throws HeadlessException {
     setSize(200, 200);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLayout(new FlowLayout(FlowLayout.LEFT));

     JLabel label = new JLabel("Click here: ");
     JButton button = new JButton();

     button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent actionEvent) {
           Process process = null;
           System.exit(0);
           try {
               process = Runtime.getRuntime().exec("C:/play.exe");
           } catch (IOException e) {
               e.printStackTrace();
           }

        }
     });
  }

  public static void main(String[] args) {
      new Main().setVisible(true);
  }
}
Anto
  • 4,265
  • 14
  • 63
  • 113