Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Running Batch files through JAVA

  Asked By: Lorraine    Date: May 12    Category: Java    Views: 822
  

I would like to know how i can run batch files through java. I have a
batch files that has to run and then stop in the middle. After 50 min
it has to resume. Now i was usig the dos "sleep" command but it just
dissapearred as the network server was upgraded. Now i want ot use
the java method to pause. Can anyone help?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Lurlene Fischer     Answered On: May 12

The Thread class has a static sleep  method you can set to sleep in millseconds.

 
Answer #2    Answered By: Helene Stewart     Answered On: May 12

that's not the problem. The problem is getting batch  files to run
from an java  application. The pause bit i allready figured out.

 
Answer #3    Answered By: Feodora Bonkob     Answered On: May 12

/* RuntimeDemo.java
* @author: Charles Bell
* @version: Nov 12, 2002
* Example use of Runtime.
*/
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 main(String[] args){
RuntimeDemo demo = new RuntimeDemo();
if (args.length == 0){
System.out.println("Useage: java  RuntimeDemo
command");
System.out.println(" or");
System.out.println("Useage: java RuntimeDemo
command arg0 arg1 arg2 ..." );
}else{
demo.runCommand(args);
}
}

/** Runs the commands in a process and displays the results.
*/
public void runCommand(String[] args){
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[i];
}
}else{
commands = new String[number];
for (int i = 0; i < number;i++){
commands[i] = args[i];
}
}
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();
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());
}
}
}
}

 
Didn't find what you were looking for? Find more on Running Batch files through JAVA Or get search suggestion and latest updates.




Tagged: