Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

calling batch files from java

  Asked By: Hisham    Date: Jun 19    Category: Java    Views: 682
  

I found this java source which will call a windows batch file from
java
this works fine when I am executing this from java , however I want
to call this from oracle

when I call the class from oracle sql plus it hangs
any ideas would be of help


I am attaching the java code here

-------------------------------------------------------------
import java.io.*;
/** Executes the String arguments, displays the commands being
executed, and displays
* any results.
*/
public class RuntimeDemo{
/** Instantiates a class and runs the commands passed in the aguments.
*/

public static void runThis(String args){

RuntimeDemo demo = new RuntimeDemo();
demo.runCommand(args);

}
/** Runs the commands in a process and displays the results.
*/
public void runCommand(String args){
int number = 1;
try{
int number = args.length;
try{
String[] commands;
String operatingsystem =
System.getProperty("os.name");
if
(operatingsystem.toLowerCase().indexOf("windows") > 0){
commands = new String[number + 2];
commands[0] = "command.com"; //for Windows 95, 98, ME. etc.
if
(operatingsystem.toLowerCase().indexOf("nt") > 0){
commands[0] = "cmd.exe"; //for Windows NT
}
commands[1] = "/c";
for (int i = 0; i < number;i++){
commands[i+2] = args;
}
}else{
commands = new String[number];
for (int i = 0; i < number;i++){
commands[i] = args;
}
}
System.out.print("Executing: ");
for (int i = 0; i< commands.length;i++){
System.out.print(commands[i] + " ");
}
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(commands);
// Because some native platforms only provide limited buffer size
// for standard input and output streams, failure to promptly write
// the input stream or read the output stream of the subprocess
// may cause the subprocess to block, and even deadlock.
CheckStream csin = new
CheckStream(process.getInputStream());
CheckStream cserr = new
CheckStream(process.getErrorStream());
csin.start();
cserr.start();
System.out.print("Waiting for command process to terminate.");
int done = process.waitFor();
System.out.println("value of done ");
System.out.println(done);
process.destroy();
System.out.println("... Done.");
}catch(InterruptedException ie){
System.out.println("InterruptedException: " + ie.getMessage());
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}
/** Inner class for checking the results if any of an InputStream.
*/
class CheckStream extends Thread{
BufferedReader br;
String lineread = "";
/** Constructor needs an InputStream to form an anonymous
* InputStreamReader which is used to create a
BufferedReader
* for reading the stream.
*/
CheckStream(InputStream is){
this.br = new BufferedReader(new InputStreamReader(is));
}
/** Reads the input stream and displays anything returned.
*/
public void run(){
try{
while ((lineread = br.readLine()) != null){
System.out.println(lineread);
}
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on calling batch files from java Or get search suggestion and latest updates.




Tagged: