Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Run a batch file in a Windows system

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try start instead of call.

Hunter
 
Marshal
Posts: 72496
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After a little google and experimentation the solution was to call



Thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 72496
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Squanch that. And squanch this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic