Home > Blockchain >  Trying to use Java Swing and Filechooser but there is an error in my code
Trying to use Java Swing and Filechooser but there is an error in my code

Time:07-09

I have a java program with a GUI as follows. What it does is it consists of a button that allows a user to select a file and on selecting the file the command prompt should open up and change to the directory of that file.

However the command prompt doesn't open up with this code. How do I modify the code so I change to the directory of the selected file?

public class GUIProject {

    public static void main(String[] args) {
        JFrame f=new JFrame();
        //final JTextField tf=new JTextField();
        JTextArea File1= new JTextArea("Select the program to test.");
        JTextArea File2= new JTextArea();
        File1.setBounds(50,50, 150,20);
        JButton b=new JButton("Select");
        b.setBounds(50,100,95,30);
        b.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                try {
                    File1();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        f.add(b);
        f.add(File1);
        f.add(File2);
        f.setSize(400,400);
        f.setLayout(null);
        f.setVisible(true);
    }

    public static void File1() throws IOException{
        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        int returnValue = jfc.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();

            ProcessBuilder builder = new ProcessBuilder(
                    "cmd.exe", "/c", "cd \"selectedFile.getAbsolutePath()\" && dir");
            builder.redirectErrorStream(true);
            Process p = builder.start();
        }
    }
}

CodePudding user response:

A process builder is not supposed to open a new window. It does this in the background by default. You can trick it to open a cmd window with using the parameter /c start. Add aother parameter to set the location of the new window: cmd /c start /D "C:/users/".

With a process builder this can look like this:

String command = "cmd /c start /D \"C:/users/\"";
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.start();

CodePudding user response:

If you want to run multible commands you can put them together with && like this:

cd "C:/users/" && echo hello world && echo I will be executed after.

The output of this would be:

hello world
I will be executed after

C:\Users>

In Java this could look like:

String[] commands = {"cd C:/users/", "echo Hello world", "echo I will be executed after"};
String command = "cmd.exe /c "   String.join(" && ", commands);
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.inheritIO(); //to see the result in the console
pb.start();

CodePudding user response:

With this code i wrote below, you can open cmd and set it on desired directory.

import java.io.File;
import java.io.IOException;

public class main {
    public static void main( String[] args ) {
        try{
            String command = "ping www.google.com";
            //remove "\"start;" to hide cmd
            String[] cmd       = new String[]{ "cmd.exe", "/C", "\"start;"   command   "\"" };
            File     directory = new File( "C:/Users" );

            ProcessBuilder pb = new ProcessBuilder( cmd );
            pb.directory( directory );

            Process process = pb.start();

        }catch( IOException e ){
            System.out.println( "Something has gone wrong" );
            e.printStackTrace();
        }
    }
}

Let me know if it was helpfull.

  • Related