views:

64

answers:

2

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);
+4  A: 

You can use this exec() :

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

Resources :

Colin Hebert
what must be given to the command string? empty?
LGAP
command must be the called executable and the second parameter is the list of parameters passed to your command. (@see the link)
Colin Hebert
+3  A: 

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.

dogbane