File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes General Computing and the fly likes Run a batch file in a Windows system Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Run a batch file in a Windows system" Watch "Run a batch file in a Windows system" New topic
Author

Run a batch file in a Windows system

Stavros Kanarakis
Greenhorn

Joined: Sep 28, 2010
Posts: 12
Hi all,

I want to be able to call a Windows batch file from my Java desktop application running at the same machine hosting the batch file.

I have the full pathname in a String named "batchFilePath" in a form C:\directory\batchfile.bat
However, when I am trying to call it, nothing happens.

I tried to change the single backslash to double backslash but nothing happens either.
Could anyone help please?

The code excerpt is below.



Thanks
Steve


Exploring Java world...
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Nothing happens, as in: nothing gets written to the log file? the program sits? can you run the batch file manually without error?

edit: Also I'm pretty sure that batch files by themselves aren't executable. They need something to run them. Try adding "cmd /c start" before the path to your batch file.


Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Stavros Kanarakis
Greenhorn

Joined: Sep 28, 2010
Posts: 12
Thanks for replying.

I have managed to run the batch files from the Windows command prompt by issuing the following command.


>call "C:\directoryNameContainingSpaces\batchfileName.bat"

But when I tried to create this script and give it as argument to the exec method, the batch file did not run as it ran from the command prompt of Windows.
That is why I removed "call" from the String containing my pathname.
Stavros Kanarakis
Greenhorn

Joined: Sep 28, 2010
Posts: 12
Thanks for replying.

I have managed to run the batch files from the Windows command prompt by issuing the following command.


>call "C:\directoryNameContainingSpaces\batchfileName.bat"

But when I tried to create this script and give it as argument to the exec method, the batch file did not run as it ran from the command prompt of Windows.
That is why I removed "call" from the String containing my pathname.
Stavros Kanarakis
Greenhorn

Joined: Sep 28, 2010
Posts: 12
I just tried the

cmd \c call "C:\directoryNameContainingSpaces\batchFileName.bat"

from the Windows Run frame and it runs successfully.

However, it does not run from the Java application.

I even added a double backslash to the same command but no luck either.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

try start instead of call.

Hunter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38763
    
  23
Are you using Runtime.getRuntime().exec()? I presume if you are, you have read Michael Daconta's classic paper.

Anyway, this is too difficult a question for "beginning", so I shall move it.
Stavros Kanarakis
Greenhorn

Joined: Sep 28, 2010
Posts: 12
After a little google and experimentation the solution was to call



Thanks for your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38763
    
  23
Beware, beware. Don't use Runtime.exec() until you have read Daconta's article about it. There is a bit about it in the documentation for Process, but Daconta gives lots more details.
Stavros Kanarakis
Greenhorn

Joined: Sep 28, 2010
Posts: 12
Campbell Ritchie wrote:Beware, beware. Don't use Runtime.exec() until you have read Daconta's article about it. There is a bit about it in the documentation for Process, but Daconta gives lots more details.


Ok thanks, I will have a look.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Run a batch file in a Windows system