aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

Not able to call a bat file from JAVA

Praveen Kumar Jayaram
Ranch Hand

Joined: Jul 14, 2008
Posts: 56
Hi

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


Praveen
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 570
    
    7

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...

SHOULD IT???
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You probably want to add

System.out.println(SystemFilesDir.getAbsolutePath()+"\\Batch.bat")

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
Rancher

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

Bill
Geo Joseph
Greenhorn

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

regards,

Geo
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: http://aspose.com/file-tools
 
subject: Not able to call a bat file from JAVA