Running an batch file under java
HI- I created a batch file to do various commands. However, every time I try to run it using:
Runtime.getRuntime.exec( BAT );
BAT being the path name and batch file in a String.
I get an IOException. Is there a way to run another command prompt in the VM so I can run my batch file? Thanks
Seigot
[335 byte] By [
seigot] at [2007-9-26 2:46:27]

I've run batch files by just giving the batch file name to exec() ( ...exec.("mybatch.bat"); ).Maybe the problem is with one of the commands in the batch file?
Hi - In my patch file I run a perl script. I tried to run a perl script using java, but still no luck. Do you know which is better to do. Using a batch file or running the perl script directly from java. thanksseigot
seigot at 2007-6-29 10:29:46 >

Hi,
this is the way.
try
{
Runtime r = Runtime.getRuntime();
r.exec(BAT);
}
catch(IOException e) {}
Thanks
Anand
> HI- I created a batch file to do various commands.
> However, every time I try to run it using:
>
> Runtime.getRuntime.exec( BAT );
>
> BAT being the path name and batch file in a String.
>
> I get an IOException. Is there a way to run another
> command prompt in the VM so I can run my batch file?
> Thanks
>
> Seigot
actually, I'm having the same problem
i'm using win2k, jdk1.3
and i've tried
cmd /c blahblah.bat
command /c blahblah.bat
cmd.exe blahblah.bat
command.com blahblah.bat
start cmd blahblah.bat
and all sorts of combination...
and it still doesn't seem to work... - it just seem to "skip over" the line and didn't start a new cmd
what envir do u have?
thx
b
It works for me on W2K and JDK 1.4 beta.
Here is the Java code:
public class Exec {
public static void main(String[] args) throws java.io.IOException {
Runtime.getRuntime().exec("foo.bat");
}
}
Here is the batch file foo.bat:
echo bar > bar.txt
Both files are in the same directory, which is the same as the current directory. When I run "java Exec", the foo.bat batch file creates the file bar.txt.
See if you can get this example to work.
Hi Schapel,
I am encounter an exception as follows when tested with your code.I am using jdk1.3
C:\jdk1.3\bin>java Exec
java.io.IOException: CreateProcess: foo error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:66)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:551)
at java.lang.Runtime.exec(Runtime.java:418)
at java.lang.Runtime.exec(Runtime.java:361)
at java.lang.Runtime.exec(Runtime.java:325)
at Exec.main(Exec.java:8)
Help me out with this,
Thanks in advance.
khiz_eng
> error=22 is the error code for "file not found". Any particular reason why?
It don't work for me at least at 1.3 win2k. But perhaps i should try with 1.4B instead. I wonder if there is no other workaround?
Hi All,Read this article hopefully it will give u answers for all your questions. http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.htmlR.Karuna