473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Execute .bat file from java

MarkoKlacar
296 Recognized Expert Contributor
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 50097
r035198x
13,262 MVP
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 Top Contributor
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 Contributor
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 Recognized Expert Contributor
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 MVP
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 Recognized Expert Contributor
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
cnarun6897
1 New Member
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
prathimmi
1 New Member
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

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

Similar topics

0
by: John Doe | last post by:
Hello, I'm currently working on a little tool that would benefit from having a little embedded script language. Although I have C++ source code available for such a script language, I have 2...
1
by: Sergio | last post by:
I want to execute a method of one applet from one page HTML. If I execute the method that it reads from rows from init() all it works. f instead I execute the method through...
3
by: Christian Blackburn | last post by:
Hi Gang, I would like to have my program use the built in Move and Copy Dialogs that are used by Windows Explorer/My Computer. Thanks in Advance, Christian Blackburn
0
by: snkssa | last post by:
Hi, I got the following error while building an application with ant. please can you give the solution or reason why it is failing? Thank you create_zip_linux: ...
4
by: ad | last post by:
I used vs2005 to develop a winform project. How can I make my project as a single executable file?
2
by: P1u70 | last post by:
I would like to decompile a *nux execute file to source file , any tools for it ? thx!
1
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in...
1
by: banging | last post by:
Hi there, I have a question regarding locking of tables so that when two or more people try to write or update the mysql tables, it locks up. Basically I only want one person to write to the...
5
by: sayeo87 | last post by:
Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got: <%@...
1
by: nike111 | last post by:
i have a package "first" with a main in one of the file. i have another package "second " with a main in it. now i want to execute these two packages from a third package named "third" ... how can i...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.