1

I am trying to run a .exe file that I have generated inside a Java code. I have a GUI written in Java and the .exe file is generated using MATLAB (its actually a Simulink model). When I run the .exe file separately (i.e. I double click on it) it will create an output file ( which is what I expect) but when I run my Java code it opens the command prompt but it won't generate any outputs at all -in fact I am not even sure if it runs my .exe file or not.

Here is my code:

package combustionModel;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUIInterface extends JFrame {  
    JButton b1 = new JButton();

    public static void main(String[] args){
        GUIInterface gui = new GUIInterface();  
    }

    static class Action implements ActionListener{
        public void actionPerformed (ActionEvent e){
            JFrame frame2 = new JFrame();
            frame2.setVisible(true);
            frame2.setSize(100, 200);
            final JFileChooser fc  = new JFileChooser();
            fc.showOpenDialog(null);
            File file = fc.getSelectedFile();
            System.out.println(file.getAbsolutePath());
            try {
                Runtime runtime = Runtime.getRuntime();
                Process p = Runtime.getRuntime().exec("cmd /c start "+file.getAbsolutePath());
                p.waitFor();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

    public GUIInterface(){
        setVisible (true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,200);
        setLayout(new FlowLayout());
        JPanel adpanel = new JPanel();
        JButton OK = new JButton();
        b1.addActionListener(new Action());
        adpanel.add(OK);
        adpanel.add(b1);
        super.add(adpanel);
    }

}
5
  • 2
    Don't use Runtime.exec(), use a ProcessBuilder! Commented Jun 28, 2013 at 12:34
  • Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces. @fge +1 Commented Jun 28, 2013 at 12:34
  • You need to consume the process inputstream, as described in the javadoc, if possible not on the UI thread. Commented Jun 28, 2013 at 12:36
  • Remember that if you do this you will no longer have java's main advantage of compatibility. Commented Jun 28, 2013 at 13:17
  • But my code doesn't take any arguments as an input -it reads a couple of files but their directory is already hard coded in the .exe file- From what I see it actually runs my .exe -from seeing what it writes on the command prompt- but for some reason it doesn't generate the output files that it is supposed to generate! Thanks for your help! @AndrewThompson Commented Jun 28, 2013 at 14:32

2 Answers 2

1

Try by passing the absolute path

example

Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
Sign up to request clarification or add additional context in comments.

Comments

1
 Process p = Runtime.getRuntime().exec("cmd /c "+file.getAbsolutePath());

try this instead

 Process p = Runtime.getRuntime().exec("cmd /c start "+file.getAbsolutePath());

1 Comment

Thanks for your help! Tried this, still don't get any output files!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.