简体   繁体   中英

Run executable from JFrame (linux) (eclipse)

I am trying to run a linux based executable called src2srcml in my JFrame GUI that takes a source file (C, C++, Java) and converts it to an XML file to be used later. My GUI successfully uses the JFileChooser to locate and select the source file.

I'm currently trying to use the Runtime.getRuntime().exec() function to run the executable but so far nothing is happening. I can run it from the command line using the commands "bash-4.1$ ./src2srcml --position sum_avg.c -o hooray.c.xml", which takes the source file sum_avg.c, converts it to XML in a new file hooray.c.xml but when I try the exact same commands in Runtime.getRuntime().exec() nothing happens. I'm not particularly familiar with the Runtime.getRuntime().exec() or ProcessBuilder classes. Do I need to navigate to where the executable is first? I also tried including the path in the first parameter, the call to the executable its self but that didn't work either.

//--- 'Run Source Code' Button---//
    JButton btnRunSourceCode = new JButton("Run Source Code");
    btnRunSourceCode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if(filePath == null){
                textArea.setText("Please Load a source file (C, C++, Java)");                   
            }
            try{

                textArea.setText("Converting Source Code to XML");
                String[] commands = {"./src2srcml", "--position ", selectedFile.getName(), " -o targetFile.xml"};
                Process src2XML = Runtime.getRuntime().exec(commands);
                src2XML.waitFor();
                }
            catch(Exception exc){/*src2srcml Fail*/}
            }
    });
    btnRunSourceCode.setBounds(612, 39, 156, 25);
    contentPane.add(btnRunSourceCode);

Currently the executable is in the workspace of my project (Eclipse). when I get everything working I would like to compile the whole program into a single executable so that the src2srcml is embedded into my executable and isn't required separately. Is this possible?

Thanks in advance for any ideas!

I am not familiar with Unix, but, don't you need to call this through the unix shell, perhaps by calling bash or other shell commands with your command array? For example the answers here

Other problems

  • Threading: you are currently asking your process to run on the Swing event thread, which if successful will cause your GUI to completely freeze and be non-usable until the process ends. You should make all calls like this background to the GUI thread such as can be achieved by using a SwingWorker object.
  • GUI mistakes: You're calling setBounds on your JButton and adding to a container that probably uses a null layout, which marks this as code from a Swing newbie, since this means that you're making inflexible GUI's that are very difficult to upgrade or enhance. Use the layout managers to place your components. The Swing layout manager will help show you how to fix this.

Regarding

when I get everything working I would like to compile the whole program into a single executable so that the src2srcml is embedded into my executable and isn't required separately.

No, this is likely not possible, unless you include the file in your jar as well as code to de-zip it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM