0

I am creating a GUI in java that requires a set of commands to be run through the command line one after the other in the background after the user selects a file and clicks a button in the GUI. The commands to be executed will include the path of the file. How can I get the command line to run in the background with a set of commands after the user selects a button?

The GUI as of now only includes the file chooser.

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

public class GUIProject {

    public static void main(String[] args) {
        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        int returnValue = jfc.showOpenDialog(null);
        // int returnValue = jfc.showSaveDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();
            System.out.println(selectedFile.getAbsolutePath()); //Instead of printing send it to cmd and perform more commands 
        }
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
abc
  • 39
  • 6

1 Answers1

0

You can try this:

String command = "cd C:\users";
Process p = Runtime.getRuntime().exec(command);

And for multiple commands you can use the && operator

Runtime.getRuntime().exec("cmd /c \"start somefile.bat && start other.bat && cd C:\\test && test.exe\"");
itapi
  • 19
  • 3