File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Not able to call a bat file from JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not able to call a bat file from JAVA" Watch "Not able to call a bat file from JAVA" New topic

Not able to call a bat file from JAVA

Praveen Kumar Jayaram
Ranch Hand

Joined: Jul 14, 2008
Posts: 56

I am trying to call a .bat file from Java.
The .bat file works perfectly if I am running it manually.

Now I need it to be called from Java. But it shows a blank console window, without executing the .bat file (it is not able to load the .bat file).

Here is the code:

Please suggest what is wrong in my code.
Thanks in advance

Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

Try removing the quotes around your path. Although it is usually necessary if you type the command as one whole, this method should remove that need. Remember, you're not passing the file as an entire parameter, but the file wrapped in quotes.

Consider the following:

When run from D:, the results are as follows:

How To Ask Questions How To Answer Questions
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 601

If you are calling a bat file, the you should not be using Java......

You should rethink what you are doing...

While it can be done...

Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Originally posted by Steve Fahlbusch:
If you are calling a bat file, the you should not be using Java......

We know precious little about Praveen's situation to pass such a judgment. If he wants to invoke a batch file from java, let's help him. If he asks if it is a good idea or not and fills in details about what he's dealing with, then we can help him make an informed decision.

[How To Ask Questions On JavaRanch]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You probably want to add


to see what it actually looks like. And it's entirely possible that those extra quotes are causing the problem; Windows CMD is really funny about quotes.

[Jess in Action][AskingGoodQuestions]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Read the JavaDocs on java.lang.Process

Note the critical point about consuming the standard out and standard err output streams. If you don't, it just sits there.

This has come up a LOT on this forum.

Geo Joseph

Joined: Nov 28, 2007
Posts: 8
Also you need to read this article from JavaWorld


G Estes
Ranch Hand

Joined: Oct 22, 2002
Posts: 62
Try this...then read through what is going on...should help...

[ September 08, 2008: Message edited by: G Estes ]
I agree. Here's the link:
subject: Not able to call a bat file from JAVA
It's not a secret anymore!