• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

execute batch files in java

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have 2 batch files "run.bat" and "run1.bat". They reside in C:/bin/. Contents of these files are as follows: Observe that "run.bat" is calling "run1.bat".

Run.bat
@echo Sanjay
call run1

Run1.bat
@echo Sanghavi

My java class reside in E;/bin and it looks like...

main(String[] args)
{
String command = "c:/bin/run.bat";
Process child = Runtime.getRuntime().exec(command);
}

Result of this java code execution is that it prints "Sanjay" and then its not able to call "run1.bat"

This is because the directory where these batch files reside is different from the dir where my java class is. If I change "run.bat" as follows, it works fine and prints "Sanghavi".

Run.bat
@echo Sanjay
call c:/bin/run1

But I dont want to change the batch file instead is there a way I can achieve this with some code changes in java or some other way to do this?

Thanks in advance
Sanjay
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

This ought to work as long as the directory in which the BAT files are located in on your PATH.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried
"cmd /c c:/bin/run.bat"

instead? Like EFH says, it should work if the bat files are in the path.

You could change the run1.bat to do (assuming windows NT or better)


set CURDIR=%~dp0
echo Sanjay
call "%CURDIR%run2.bat"

Then provided both run1 and run2 are in the same directory, you should be able to put them anyway.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic