[an error occurred while processing this directive]



Executing programs from Java applications

Thanks to the exec() method of the Runtime class, it is possible to execute extern programs from a Java application. In order to do it a new Process object is instantiated and it is used to execute the specified command. This strategy allows us to have a full control on the new executing process: we are allowed to decide if the basic application has to continue his processing, or wait untill the extern program exits. If necessary it is fossible to kill the son-process too. It is important to underline that Java applications which uses the exec() method lose their portability. If we in the code we are going to execue a command like exec("ls"), the application will not work on non-Unix platforms. To run it on Windows platforms we should change the "ls" command with the "dir" one. The code below, written on Windows98, executes a command in a new shell, saves its output to a file (output.txt). When the son process exits the main program shows the output.txt file in the current window.





import java.io.*;

public class Example {

public static void ExecuteCommand (String ExternCommand) throws IOException
{ Process proc = Runtime.getRuntime().exec("start command /c "+ExternComand+" >output.txt");
char c;
try { proc.waitFor(); }
catch (InterruptedException e) { System.out.println("InterruptedException raised: "+e.getMessage()); }
InputStream f = new FileInputStream ("output.txt");
while ((c=(char)f.read())!=(char)-1)
System.out.print(c);
}

public static void main (String args[]) throws IOException
{ String ExternCommand = new String();
int k;
for (k=0;k<args.length;k++) { if (k>0) ExternComand = ExternCommand + " ";
ExternCommand = ExternCommand + args[k];
}
try { System.out.println("Command: "+ExternCommand);
ExecuteCommand(ExternCommand);
}
catch (IOException e) { System.err.println ("IOException raised: "+e.getMessage()); }
}

}



According to the operative system we are using it could be necessary to change some lines in the ExecuteCommand() function. I've not tested this code on many different patforms, so I'm not sure it will work correctly on any system. The line we have to change according to the computer's operating system and version is:


Process proc = Runtime.getRuntime().exec("start command /c "+ExternCommand+" >output.txt");


The "/c" option, which follows the "command" instruction, means the new shell has to exit when the process ends his execution. With some Windows versions it is necessary to change the line as follows:


Process proc = Runtime.getRuntime().exec("command /c "+ExternCommand+" >output.txt");


and if necessary to sostitute the name of the command interpreter (Generally it is "COMMAND.COM" or "CMD.COM"). On Unix (or Linux) platforms we shouldn't start a new shell, so we could write:


Process proc = Runtime.getRuntime().exec(ExternCommand+" >output.txt");


On the other hand, if we need to execute the extern command on a different shell we'll write:


Process proc = Runtime.getRuntime().exec("sh "+ExternCommand+" >output.txt");


Where "sh", is the shell's name. When we start a new shell we can't be sure, on both Windows and Unix/Linux systems, its enviroment variables (like the PATH and many ohers) are maintained. This mean it is possible that the required application will not be found in the local file system or it will not work correctly. To solve the path problem, as example, we have to specify the full command's path in the exec() method. With some Windows systems we have to use the start command you can find in the \windows\command directory.

That's all... I hope, if you'll decide to use this strategy in your application, yo'll tell me about problems and seccesses....




Italia HyperBanner Network