• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run a batch file (.bat) from java programm (.java)

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i sufur a problem..i tried it a lot but couldnt find to manage this..
i run this code

import java.lang.Process;

public class Rbat {

public static void main (String args[]){
try{
Runtime rt = Runtime.getRuntime();
Process P=null;

P = rt.exec("D:/Rahul_A/test.bat");
}
catch (Exception e){
e.printStackTrace();
}
}
}

its not working
please suggest me what can i do now to run this bat file
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say it doesn't work, do you mean there is no output? A Process will not put its output on standard out. You will have to read its streams.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaDocs for java.lang.Process have quite a nice discussion on this, have you consulted them?
Bill
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i tried it..
its not giving any exception but its not running the .bat file also.

i tried to run the test.bat file seperatly its fine, works properly
but when i execute that by java programm its not running.

looking ahead..
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Shilpakar:

its not giving any exception but its not running the .bat file also.


The remote process will not throw exceptions. This is a good article on the common problems using exec(). As Keith points out, you should be reading the output from the process. You should also check the exit code of the process.
There are some things that just aren't well-suited for running with exec(). What are you trying to do?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running .bat files as a Process should be easy if you pay attention to:
1. consuming the standard out stream in a separate Thread
2. consuming the standard err stream in a separate Thread (NOT the thread in 1)
3. constructing the command line properly (so many alternatives!!!)
4. avoiding dependence on the "current" directory

Bill
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if suppose i have a dos command in batch file like one:

md Newfold

then it should have make a new folder on the curret drive! but its not

:roll:
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That line looks like you are doing precisely what William cautions against in his point #4. Use absolute paths instead.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
This is a good article on the common problems using exec().


More specifically, take a look at "Assuming a command is an executable program" on page 3. It's exactly describing your problem (the problem with bat-files is similar - they aren't programs, either).
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes , its working fine. now i can run .bat from .java.
the thing is that the commands inside the batch file need to mention properly. i was been trying relative path to create the folder in command inside .bat file.

we need to mention Absolute path.

thank you!

**********

The second thing is that if my application is running on both windows and linux system. now on linux system we have other extension of batch file. how to handle this at a time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic