Add to Google Reader or Homepage

how to create a batch file using java


Batch File:

  • In this post i am going to show you the implementation of java program to create a batch file.
  • We might all wonder about "what is a batch file?","what a batch file contains?". The batch file contains only the DOS commands which we would type in the command prompt.
  • If you have seen any application,the installation package of the software may contain one or more batch files.These batch files are designed to perform the predefined tasks in the background.     
  • We can able to configure the batch file on our own to perform the tasks that we want to do.
  • For eg. Initially,when we start to learn java we used the command prompt to compile and execute our program.Before compilation we may have to configure some settings like setting the class path,path etc.
  • These type of settings can be easily configured by using the batch file rather than typing the commands manually.  
The java code for creating the batch file is shown below.

Program:

import java.io.*;
import java.util.*;
import java.lang.*;

public class Bat
{

public Bat()
{
}
public static void main(String args[]) throws Exception
{
Batch b=new Batch();
b.createBat();
b.executeBat();
}
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class Batch
{
public Batch()
{
}
public void createBat() throws Exception
{
File file=new File("C:\\manet\\Execute.bat");
fos=new FileOutputStream(file);
dos=new DataOutputStream(fos);
dos.writeBytes("CD \\");
dos.writeBytes("\n");
dos.writeBytes("CD Project Document");
dos.writeBytes("\n");
dos.writeBytes("SET CLASSPATH=c:\\Program Files\\Weka-3-7\\weka.jar;");
dos.writeBytes("\n");
dos.writeBytes("cd ..");
dos.writeBytes("\n");
dos.writeBytes("ECHO $GANESH");
dos.writeBytes("\n");
dos.writeBytes("CD \\ ");
}

public void executeBat() throws Exception
{
String cmd="cmd /c start c:\\manet\\Execute.bat";
Runtime r=Runtime.getRuntime();
Process pr=r.exec(cmd);
}
FileOutputStream fos;
DataOutputStream dos;
}

The above program will create a batch file named "Execute.bat" and it will start reading the contents of the file as commands and execute it in the console(command prompt).

5 comments:

Nikos Maravitsas said...

Hi Ganesh

Nice blog! Is there an email address I can contact you in private?

Ganesh said...
This comment has been removed by the author.
Unknown said...

awsome piece of information, I had come to know about your website from my friend vinod, indore,i have read atleast seven posts of yours by now, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a ton once again, Regards, How to create batch file

shashi kumar said...

One of the best online blog ever I have come across...

Unknown said...

Hi I had a main method in a class..I need to execute this main method through batch file .Please help me out.
Thank You.

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top