This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to execute .BAT thru java Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to execute .BAT thru java Application" Watch "How to execute .BAT thru java Application" New topic
Author

How to execute .BAT thru java Application

Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Hi,
I need to run .BAT file from Java application.
here is the code I am following:
class test....{
try{
Runtime.getRuntime().exec("Jtier.BAT");
}catch..
There are three commands to execute in the .Bat file
SET PATH=%PATH%;C:\j2sdk1.4.0\bin;
SET CLASSPATH=.;C:\j2sdk1.4.0\classes;C:\j2sdk1.4.0;C:\mq.jar;
java Jtier

But it dosent work. It dosent execute hte JTier application, when I run test java program.
But when I change the code as:
class test....{
try..
Runtime.getRuntime().exec("java Jtier");
}catch..
It works.
Please help me to understand how it works. Does my .bat file should have only one command to execute? Can this not execute mulitple commands?
Please let me know.
Thanks,
Manoj


Manoj<br />SCJP, SCJD, SCWCD, IBM WSAD.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Is JTIER.BAT on the PATH when "test" is executed?


[Jess in Action][AskingGoodQuestions]
Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Hi Ernest,
Actually .BAT file is in the same folder where the test.class is. It executes the .BAT file, but only first command from .BAT file and not all the commands. That's the real problem.
Thanks,
Manoj
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

This is a silly question, but does the BAT file work if you just run it from the command line?
Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Hi Ernest,
Yes the .bat executes like any normal bat files. You can also try this code to verify:
try{
Runtime.getRuntime().exec(*);
} catch(IOException e) {System.out.println("exception");}

* : Put your own .bat file here and have at least 2 commands in it. Once you run this app, check if the second command from the .bat was executed or not.
Thanks for your replies..
manoj
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, I would try it if I had a Windows machine anyplace. Thankfully, I don't!
Can't imagine what the problem might be -- anybody?
Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Jim Thanks for your concern. But I really need immidiate help on this.
Somebody help me with this
Manoj
[ September 25, 2003: Message edited by: Jmannu gundawar ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Who's Jim?
Anyway, something about your plight just sounds so... sad, so I felt I had to rush to your aid. Stuck on a sad little Windows machine, forced to work with .BAT files
I went over and fired up my wife's XP machine, wrote this little batch file, foo.bat:

If you run this, you get two notepad windows, one appearing as soon as the other is closed.
And I wrote this little Java program:

And when I ran it, I got a "CreateProcess: err=3" exception. OK, well, I know that batch files are like UNIX shell scripts, so maybe you've got to run the command processor. So I tried

And the exception went away, but nothing happened. So then I said, "Well, maybe the JVM is exiting before the process is actually started." So I tried this:

And, ta-da! It worked! So, in summary: first, you have to actually run cmd.exe and tell it to execute the batch file (that's just how Winders works, I guess; the CreateProcess OS call isn't smart enough to do this) and you have to make sure your program doesn't exit before the external process runs.
Now, surely, somebody else could have told you this sooner, what with my pitifully limited Windows experience. But anyway, hope that helps.
Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Ooooooo Laa Laa...It worked.
Ernest, thanks a lot for this new finding. It never flashed in my mind. I really appriciate the way you helped me out. And convey my thanks to your wife too, for letting u use her machine to help me out
And just to add a note...cmd part is not really required. It did not work for you in the second attempt just because it did exit the program before it could do anything. That was the only reason I guess.
And sorry about calling u Jim. Jim is Jim Yingst. I have another thread (on some other topic) in developer forum, and so the names got mixed up.
Thanks again,
Manoj
Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
Hi,

I want to run a bat file from the java class written using Websphere Studio Appl Developer.My piece of code is inside a method that is called as shown below:

public void finish() {
String cmd = "C:\\temp\\EAB_RECORD\\EAB\\Record.bat";
cmd += " " + className;
cmd += " " + dirName;
cmd += " " + packageName;
cmd += " " + commAreaName;
cmd += " " + fileName;
byte[] b = cmd.getBytes();
try{
File f = new File("C:\\temp\\EAB_RECORD\\EAB\\abc.bat");
FileOutputStream fo = new FileOutputStream(f);
fo.write(b);
fo.close();
f=null;
System.out.println("b4 exec abc.bat");
Runtime.getRuntime().exec("cmd /C C:\\temp\\EAB_RECORD\\EAB\\abc.bat");
Thread.sleep(12000);
System.out.println("after exec abc.bat");
}catch(Exception e){
e.printStackTrace();
}
}

As seen above I have also used Thread.sleep as given in the above postings.But the bat file doesnt seem to do the intended task which is to create the directory structure and create some classes that are put in those directories.I am using Windows XP OS and am using WSAD to code the program...Please suggest some way to execute the bat file successfully.
[ May 16, 2007: Message edited by: Nikhil Sun ]
Siva Anandhi Rajendran
Greenhorn

Joined: Apr 08, 2007
Posts: 2
Runtime.getRuntime().exec(String) returns an object of Process.
Process has a method waitFor() which allows the current thread to wait until the process represented by it's object is completed.
Despite of using thread.sleep() you can use waitFor() as,
Process proc = Runtime.getRuntime().exec("someFile.bat");
proc.waitFor();
Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
Thanks Siva it worked now
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Ben Terova,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to execute .BAT thru java Application
 
Similar Threads
Why is Runtime.getRuntime().exec() not working in JSP?
How do you call a .bat file from a Java program
executing dos commands in java
run bat file within java
problem running .BAT file from the Java app