cryptiblinux
cryptiblinux

Reputation: 27

How to use processBuilder in Java to execute an exe with arguments in Windows Command Prompt

This is the code I've been using:

            ProcessBuilder process = new ProcessBuilder("C:\\Users\\path\\to\\exe\\my_exe.exe ",
                "my_exe.exe", "/removeDrive", "driveLocation");
            process.start();

All this seems to do is run the exe, but I can't get it to run the command I want.

The command in CMD would be:

C:\users\path\to\exe>my_exe.exe /removeDrive driveLocation

The command works fine in windows Command Prompt

Upvotes: 0

Views: 5214

Answers (1)

user5364144
user5364144

Reputation:

Another way to do the same thing:

ProcessBuilder pb = new ProcessBuilder(
        "cmd", "/c", "path/to/exe.exe", 
        "/removeDrive", "driveLocation");
pb.start();

Or if you don't need output:

Runtime.getRuntime().exec("cmd /C my_exe.exe /removeDrive driveLocation");

Upvotes: 2

Related Questions