0

The title basically says it all. What I've been able to do so far, by searching around on the web, is the following:

Runtime rt = Runtime.getRuntime();

try {
    Process proc = rt.exec("cmd /c start cmd.exe /K \"cd " + locaction);
} 
catch (Exception e) {
    //...
}

where location is the String representation of the directory I'd like to switch to. Not sure if the above is the best way to do that, but either way, how do I then run a certain command from that directory (say, e.g., there's an application there and I want it to run)? Thanks.

4
  • 1
    Any specific reason why you want to run from the console? Commented Jun 7, 2013 at 23:26
  • The change directory call will not work as expected when using a Runtime instance. You should specify the directory as part of the program name. Commented Jun 7, 2013 at 23:56
  • @fge : I'm creating an executable jar that makes a directory, puts a certain file there, and then runs a script from that location. The script is best run with a simple command from the command line. Commented Jun 9, 2013 at 1:34
  • @Legend : the code ran fine for me. are you saying its just not a preferred way to do it? Commented Jun 9, 2013 at 1:35

1 Answer 1

3

If you just want to run an application with a specific working directory, the easiest way is to use a ProcessBuilder:

ProcessBuilder pb = new ProcessBuilder(executable, arguments, if, any);
pb.directory(theWorkingDirectory);
pb.start();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.