aspose file tools*
The moose likes Distributed Java and the fly likes create and execute a batch file through a java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "create and execute a batch file through a java program" Watch "create and execute a batch file through a java program" New topic
Author

create and execute a batch file through a java program

Nivedhitha Bhoopathi
Greenhorn

Joined: Nov 06, 2009
Posts: 8
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

You need a new line character while writing to the file




Swastik
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
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

Joined: Nov 06, 2009
Posts: 8
Hi All,
"\n" works...

thanks..
Nivedhitha Bhoopathi
Greenhorn

Joined: Nov 06, 2009
Posts: 8
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

Joined: Mar 22, 2000
Posts: 12789
    
    5
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
Marshal

Joined: Mar 22, 2005
Posts: 41904
    
  63
In particular, read the article "When Runtime.exec() won't" which talks about the various pitfalls of that method.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: create and execute a batch file through a java program