Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Executing a .bat file from my code

Reply
Thread Tools

Executing a .bat file from my code

 
 
Meidan
Guest
Posts: n/a
 
      12-27-2005
Hi,

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

Thanks.

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      12-27-2005
Meidan wrote:

> Is there a way to execute a .bat file from within my java code?


<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String)>
(and overloaded variants) though it might make more sense
to 'exec' the individual commands in the .bat file of interest.

HTH

--
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-27-2005
On 27 Dec 2005 01:12:52 -0800, "Meidan" <> wrote,
quoted or indirectly quoted someone who said :

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


see http://mindprod.com/jgloss/exec.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Shameek
Guest
Posts: n/a
 
      12-28-2005
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.

 
Reply With Quote
 
Guillaume
Guest
Posts: n/a
 
      01-02-2006
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");

--
My desktop is worth a million of dollars. Put an icon on it.
http://www.milliondollarscreenshot.com/
 
Reply With Quote
 
Simon OUALID
Guest
Posts: n/a
 
      01-03-2006
Guillaume wrote:
> 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");
>


Or cmd.exe for win2k / xp.

More information on how to make a good use of the Runtime's exec method
here :

http://www.javaworld.com/javaworld/j...229-traps.html
 
Reply With Quote
 
vivek.india30@gmail.com vivek.india30@gmail.com is offline
Junior Member
Join Date: Jun 2008
Posts: 1
 
      06-26-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a static non-main operation in a jar file contained withina war file O.B. Java 0 06-24-2006 05:47 PM
Having a problem using WebRequest inside code invoked by the Page_Load event to to download a page located on same server (as the currently executing page). Hasani \(remove nospam from address\) ASP .Net 9 09-29-2004 05:07 PM
executing client side code from code behind. =?Utf-8?B?QXNoYQ==?= ASP .Net 1 08-12-2004 06:15 AM
Problem executing a BAT file (or EXE file) using Perl as CGI script under Apache. Tom Salzmann Perl Misc 3 07-16-2003 01:06 PM
Session end event fires but it is not executing code Sampriti ASP .Net 4 06-26-2003 10:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57