LGAP
LGAP

Reputation: 2453

Java program for changing the directory of command prompt

I have written a java program named Automate.java, in which the another java program named newsmail will be executed.

The problem i face here is, Automate.java is in Desktop location(should be in desktop only always due to some requirements) and newsmail is in /home/Admin/GATE521/LN_RB this location.

What must be done before the below code, such that the command prompt automatically goes to the required folder and executes the program.

String command = "java newsmail";
Process child = Runtime.getRuntime().exec(command);

Upvotes: 0

Views: 3562

Answers (2)

dogbane
dogbane

Reputation: 274532

Use the new ProcessBuilder class, instead of Runtime.exec().

ProcessBuilder pb = new ProcessBuilder("java", "newsmail");
pb.directory("/home/Admin/GATE521/LN_RB");
pb.start();

You can even look at pb.environment() to change environment variables if necessary.

Upvotes: 3

Colin Hebert
Colin Hebert

Reputation: 93157

You can use this exec() :

Process child = Runtime.getRuntime().exec(command, null, new File("/home/Admin/GATE521/LN_RB"));

Resources :

Upvotes: 5

Related Questions