Executing Commands within Java Successfully!
To run a command within a Java Class you need to get runtime and call exec method like this:
Runtime.getRuntime().exec("Some command...");
You can add parameters in your command string, it is possible to do this work by some other similar ways.These were what you will find if search on net about running commands in Java but some parts are missed!
Maybe you just need to run command to do something, for example to pass a query to SQL Plus (in case of mine) and you need just result of command(in this case running query), but your command may have some output result, I mean what you see during direct executing in command line. You do not need it but the process will wait to print its out put and process will not exit never and it is a big problem especially when you want run more than one command.
What you need is just defining an Process, InputStream, InputStreamReader and BufferedReader to read output buffer like this:
Process process =Runtime.getRuntime().exec("Some Command whith/without parameters");
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
// You can print it to see result.
System.out.println(line);
}
Also to be sure about exiting process you can print check exit value of process:
process.exitValue()Normal exit value is zero.






