469,270 Members | 1,109 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Execute .bat file from java

MarkoKlacar
296 Expert 100+
I need to run a .bat file from java. This is what I got:

public class CallingBatch {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
run.exec("cmd start /c C:/batfile.bat");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("FINISHED");
}
}

Now, the thing is I don't get any error's or any other type of feedback, nothing happens.

Any suggestions?
Sep 11 '07 #1
8 49549
r035198x
13,262 8TB
I need to run a .bat file from java. This is what I got:

public class CallingBatch {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
run.exec("cmd start /c C:/batfile.bat");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("FINISHED");
}
}

Now, the thing is I don't get any error's or any other type of feedback, nothing happens.

Any suggestions?
1.) Use code tags when posting code.
2.) What was the program supposed to do?
Sep 11 '07 #2
dmjpro
2,476 2GB
I need to run a .bat file from java. This is what I got:

Expand|Select|Wrap|Line Numbers
  1. public class CallingBatch {
  2.  
  3. public static void main(String[] args) {
  4. Runtime run = Runtime.getRuntime();
  5. try {
  6. run.exec("cmd start /c C:/batfile.bat");
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. }
  10. System.out.println("FINISHED");
  11. }
  12.  
  13. }
  14.  
Now, the thing is I don't get any error's or any other type of feedback, nothing happens.

Any suggestions?
Expand|Select|Wrap|Line Numbers
  1. Process p = run.exec("C:/batfile.bat"); //Correct this line.
  2. //Add this line.
  3. System.out.println(p.exitCode());
  4.  
Good Luck.

Kind regards,
Dmjpro.
Sep 11 '07 #3
questionit
553 512MB
Just a bit on executing a batfile.

.bat file is self-executable. so you dont need to open command prompt first to run .bat file.

If you do want to open cmd to run a .bat file or any other file, then try this example:

Expand|Select|Wrap|Line Numbers
  1. Process p = run.exec("cmd /k ipconfig");
  2.  
The above argument in run.exec will run cmd first and then run ipconfig.

Qi

Expand|Select|Wrap|Line Numbers
  1. Process p = run.exec("C:/batfile.bat"); //Correct this line.
  2. //Add this line.
  3. System.out.println(p.exitCode());
  4.  
Good Luck.

Kind regards,
Dmjpro.
Sep 11 '07 #4
MarkoKlacar
296 Expert 100+
Thanks for replying.

The program was supposed to call a batch file that does some basic stuff (just copy's some files, but that's not the important thing running the batch file is).

When I added the lines I got this message:

Expand|Select|Wrap|Line Numbers
  1. java.lang.IllegalThreadStateException: process has not exited
  2.     at java.lang.ProcessImpl.exitValue(Native Method)
  3.     at callingBatch.CallingBatch.main(CallingBatch.java:10)
Sep 11 '07 #5
r035198x
13,262 8TB
Thanks for replying.

The program was supposed to call a batch file that does some basic stuff (just copy's some files, but that's not the important thing running the batch file is).

When I added the lines I got this message:

Expand|Select|Wrap|Line Numbers
  1. java.lang.IllegalThreadStateException: process has not exited
  2.     at java.lang.ProcessImpl.exitValue(Native Method)
  3.     at callingBatch.CallingBatch.main(CallingBatch.java:10)
Why don't you go through a tutorial on how to use Runtime.exec first?
You are just falling into the most common and simple mistakes that most people make when using it.
Sep 11 '07 #6
MarkoKlacar
296 Expert 100+
Why don't you go through a tutorial on how to use Runtime.exec first?
You are just falling into the most common and simple mistakes that most people make when using it.
Ok, I'll try that. Thanks for pointing me in the right direction.
Sep 11 '07 #7
Hi folks...
I too had the same problem. I first used as
Runtime.getRuntime().exec("cmd.exe start /c test.bat");
Then I tried as below. It works fine.
Runtime.getRuntime().exec("cmd.exe /c start test.bat");

Try this... Have fun...
Jan 28 '10 #8
HI All,

I have to convert the .DBF to .CSV file by using DBF2CSV.bat.
in JAVA.
After googled i got the following code . But it is not working ...


Please help me
String[] commands={"C:\\DBF2CSV\\DBF2CSV.BAT","C:\\DBF2CSV\ \J006MD30.DBF"};
Process p = Runtime.getRuntime().exec(commands);
Jul 22 '10 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by John Doe | last post: by
1 post views Thread by Sergio | last post: by
4 posts views Thread by ad | last post: by
1 post views Thread by jaimemartin | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.