473,221 Members | 1,757 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,221 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 50074
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 Im 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
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.