0

How to change content of batch file using Java code?

I worked with parsing XML using Java program. It worked fine. But can I do same for the batch file using Java?

I am able to run batch file using below code.

String command = "cmd /c start " + batFile;
Runtime rt = Runtime.getRuntime();
rt.exec(command);

The content of my batch file is:

@echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C "C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml"
exit

But before doing this I want to change location

C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml

..to user given XML location. How can I achieve this in my Java application?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2215139
  • 1,805
  • 2
  • 18
  • 24

2 Answers2

2

Maybe use an environment variable instead. This way you don't need to edit the batch file, just set the variable before running it.

Here or here see how to set an environment variable from java and here's how to use them in a batch file.

Community
  • 1
  • 1
dratewka
  • 2,104
  • 14
  • 15
1

How about passing command line args to the bat file like so:

@echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C %1
exit

Then modify your java code to pass in the XML file name after the bat file name

cmbaxter
  • 35,283
  • 4
  • 86
  • 95
  • U mean me to excecute somting like with following code? String command = "cmd /c start " + batFile +" C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml"; Runtime rt = Runtime.getRuntime(); rt.exec(command); – user2215139 May 15 '13 at 10:25
  • Yes. Sorry for not posting the java code. I was answering this from my phone =) – cmbaxter May 15 '13 at 10:42
  • sorry sorry it worked for me thank you for your example answer – user2215139 May 15 '13 at 11:05