Executing a .bat file from my code

M

Meidan

Hi,

Is there a way to execute a .bat file from within my java code?
Something like WinExec?

Thanks.
 
S

Shameek

I have a sample code which shows how to run a .bat file (or any
executable file from java). :

import java.io.*;
class test{
public static void main(String arg[]){
try{
String command = "cmd /C start C:/test.bat ";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

}catch (IOException e) {
e.printStackTrace();
}
}
}

Here the .bat file is present in c:\test folder.
Hope this will b of help to u.
 
G

Guillaume

Meidan:
Is there a way to execute a .bat file from within my java code?
Something like WinExec?

Something like:
Runtime.exec("command.com /c your.bat");
 
Joined
Jun 26, 2008
Messages
1
Reaction score
0
HI Guys
I have tested following code this working on Win..XP, over java 1.5++.
To test this code you need to create three different files.
1. RunBatFile.java
2. RunByJava.bat
3. testFile.bat

I hope it will help us to see execution of bat file using java.
All the best.

package batfile;
import java.io.*;
class RunBatFile{

public static void main(String args[]){

try {
Runtime rt = Runtime.getRuntime();
rt.exec("RunByJava.bat");
System.out.println("Process exitValue: ");
} catch (Exception e) {
System.out.println("Unexpected exception Trying to Execute Job: " +e);
}
}
}


*************************************
RunByJava.bat
-------------------------
echo off
ECHO Starting TCP Monitor for Blue Martini
START/MIN CALL testFile
***************************
testFile.bat
----------------------
set tDate=%DATE%

ECHO todays date is %tDate%
SLEEP 5
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top