• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

create and execute a batch file through a java program

 
Nivedhitha Bhoopathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I want to create a batch file through java program in one method and execute it in another method.
createBat()
{
File file=new File("C:\\samplenote.bat");
fos=new FileOutputStream(file);
dos=new DataOutputStream(fos);
dos.writeBytes("ECHO happy >> myBatch.txt");
dos.writeBytes("START note.txt");
}

executeBat()
{
String cmd="cmd /c start c:samplenote.bat";
Runtime r=Runtime.getRuntime();
Process pr=r.exec(cmd);
}



when i want to create a batch file with more than one command . all commands in the batch file is written in the same line.
like this
samplenote.bat
*************
ECHO happy >> myBatch.txtSTART note.txt



but i want like the below one

samplenote.bat
*************
ECHO happy >> myBatch.txt
START note.txt

what should i for that.
is there any command to write commands in batch file in consecutive lines
 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a new line character while writing to the file


 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all you do is

you are in for frustration.

See the JavaDocs for Process about consuming the std out and std err streams the execution of your batch file will generate.

Bill
 
Nivedhitha Bhoopathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
"\n" works...

thanks..
 
Nivedhitha Bhoopathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

I can't get what you are saying.What problem in using a Process?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you do what I said and read the javadocs for java.lang.Process ?

If so you will know about the necessity of consuming std out and std err streams, each in its own Thread.

This comes up alot on ranch forums try searching.

Bill
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In particular, read the article "When Runtime.exec() won't" which talks about the various pitfalls of that method.
 
Boobalan Rajaram
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nivedhitha Bhoopathi wrote:Hi All,
"\n" works...

thanks..


Hi Nivedhitha.
i had tried from your way. but it does not work. please let you tell how i can write new line.. urgent.... thanks in advance......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic