473,120 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,120 software developers and data experts.

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 50064
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

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...
2
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
3
by: efe2023 | last post by:
Hello Everybody, There is SQL Server 2008 R2 database with MS Access front-end (linked ODBC tables). I can see all linked tables in MS Access and can add new rows. However, when trying to update...
3
by: jimatqsi | last post by:
So, I had this bright idea today that didn't quite work out. Or maybe it is working out but requires me to change a habit. I'm working on a form that will be a sub-form of another form. During...

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.