Faz
Faz

Reputation: 1

When I open CMD from Java, I cannot change the Directory

I have a problem with my Java program where I have a button that opens the command prompt and opens a batch file to run a series of commands. To do this, I need to change directory. Here is my code:

private void CommandPromptButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    try {
        new java.lang.ProcessBuilder("cmd.exe").start();
        java.lang.Runtime.getRuntime().exec(new String[]{
            //I need to change the directory in command prompt and I do not want to use escape
            "cmd.exe","/c","start","cd C:\Users\Faz"
        });

    } catch (IOException ex) {
        Logger.getLogger(TMISGUIInstallerPage.class.getName()).log(Level.SEVERE, null, ex);
    }
}      

Any suggestions and advice are appreciated.

Upvotes: 0

Views: 1630

Answers (4)

Aukta
Aukta

Reputation: 403

The following code should work

Process p = Runtime.getRuntime().exec("cmd.exe /c start cd \"C:\\Users\\Faz\" && dir");

Upvotes: 1

Faz
Faz

Reputation: 1

Thanks for all of your help and suggestions. I have finally found an answer. I forgot to add that I tried using Java runtime but that does not run all commands. I have found that if I add another quotation mark, I can change the directory.

private void CommandPromptButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
try {
    new java.lang.ProcessBuilder("cmd.exe").start();
    java.lang.Runtime.getRuntime().exec(new String[]{
        //I need to change the directory in command prompt and I do not want to use escape
        "cmd.exe","/c","start","cd C:\"Users\"Faz"
    });

} catch (IOException ex) {
    Logger.getLogger(TMISGUIInstallerPage.class.getName()).log(Level.SEVERE, null, ex);
}

}

Thank you for all of your help. Probs submit some more questions later. Cheers!

Upvotes: 0

Hearen
Hearen

Reputation: 7828

Upvoted Aukta's answer, it should solve your problem.

But as you asked:

To do this, I need to change directory.

Actually with ProcessBuilder and its directory(File directory), we can easily set the working directory. Here is a simple demo to list all files in a specified directory to show you how it can be used.

public static void main(String... args) {
    ProcessBuilder processBuilder = new ProcessBuilder("ls"); // pass in your command and options;
    processBuilder.directory(new File("/home")); // specify you directory here;
    try {
        Process process = processBuilder.start();
        String line = null;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException ignored) {
        ignored.printStackTrace();
    }
}

The output:

gitlab-runner
hearen
ubuntu

Upvotes: 0

Prashant
Prashant

Reputation: 5383

You could change the directory in the ProcessBuilder using the ProcessBuilder#directory() and then start the process. Here is a sample code:

ProcessBuilder start = new ProcessBuilder("cmd.exe", "/c", "start");
        start.directory(new File("C:\\Users"));
        start.start();

Upvotes: 0

Related Questions