aspose file tools*
The moose likes Java in General and the fly likes run a batch file (.bat) from java programm (.java) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "run a batch file (.bat) from java programm (.java) " Watch "run a batch file (.bat) from java programm (.java) " New topic
Author

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

Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
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


Perform for today. Adapt for tomorrow.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
The JavaDocs for java.lang.Process have quite a nice discussion on this, have you consulted them?
Bill
Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
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

Joined: Oct 29, 2001
Posts: 8873
    
    8

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?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
William Brogden
Author and all-around good cowpoke
Rancher

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

Joined: Aug 29, 2006
Posts: 132
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
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
That line looks like you are doing precisely what William cautions against in his point #4. Use absolute paths instead.


Ping & DNS - my free Android networking tools app
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run a batch file (.bat) from java programm (.java)