• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

create and execute a batch file through a java program

 
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need a new line character while writing to the file


 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
"\n" works...

thanks..
 
Nivedhitha Bhoopathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In particular, read the article "When Runtime.exec() won't" which talks about the various pitfalls of that method.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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......
 
reply
    Bookmark Topic Watch Topic
  • New Topic