Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run a batch file in a Windows system

 
Stavros Kanarakis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stavros Kanarakis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try start instead of call.

Hunter
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a little google and experimentation the solution was to call



Thanks for your help.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic