MarcL
MarcL

Reputation: 3593

Open cmd.exe from JAVA with PATH Presets

I want to open my cmd.exe by a click on a Button and with a String path that I give to my cmd.exe as a parameter (open the cmd with that path set)

String path = getCurrentFolderName().toString();   
ProcessBuilder b = new ProcessBuilder();   
b.environment().put("PATH", path);    
b.command("cmd", "/c", "start", path)               
b.start();  

this so far opens only the folder in a new window in Windows... how can I open my cmd.exe and pass a path to it?

Upvotes: 0

Views: 960

Answers (2)

Mike
Mike

Reputation: 23

In the past when I need to do something like this I would create and delete batch files. Use PrintWriter to create your .bat file, you can add in whatever variables you need at this point

Then to run the .bat

Runtime.getRuntime().exec("cmd /c start build.bat");

and delete it afterwords if its not needed.

Maybe not elegant, but it worked well for me before.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159844

Few changes necessary

Result

ProcessBuilder b = new ProcessBuilder();
b.directory(new File(path));
b.command("cmd", "/k", "start"); 

Upvotes: 1

Related Questions