Reputation: 51
I have written some code for executing .bat
file. which contains some
commands like setting java classpath,etc
..And finally there is one command
which runs a Java class file.The HelloWorld class
converts some xml file and generating a new xml file in some folder. When I double click .bat file, it executes fine,
but when I try to run I am not getting any output as I was getting through
double click the .bat
file. How to make a batch execute and probably it would be nice
if I could see the results through Java console.
Following is MyJava code to execute the .bat file
public void run2() {
try {
String []commands = {"cmd.exe","/C","C:/MyWork/Java/classes/run.bat"} ;
Process p = Runtime.getRuntime().exec(commands);
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
And below the some commands which has been set to .bat file
set CLASSPATH=%CLASSPATH%;C:/MyWork/Java
set CLASSPATH=%CLASSPATH%;C:/MyWork/Java/classes
java -cp test.jar;test2.jar test.HelloWorld
Tried with "/C"
commad as well. It does not execute. Actually it does not give effect of double click the .bat
file. Is there any other way that I can try with?
I can see the contents inside the .bat
file through Eclipse console. But it does not give the desired output. Desired output means when I double click .bat
file, it executes well. But through java
call, I can see the contents only .
Upvotes: 3
Views: 4507
Reputation: 36339
You do not read the error output of your batch file, therefore, you'll never see any error messages printed from there or from CMD.EXE itself. In addition, the sub-program may stall and just wait for you to read the error stream.
Please see related discussions here: How to make a java program to print both out.println() and err.println() statements?
Upvotes: 0
Reputation: 14154
Windows uses \ backslash for Windows and MS-DOS path delimiter. Forward slash / is accepted by Java in the java.io
package and translated to be a path delimiter, but will not be directly acceptable to Windows or accepted by the cmd.exe
shell.
You may also need to specify either the working directory for the batch file to be executed in, or possibly a full path to the cmd.exe
command interpreter.
See: Runtime.exec (String[] cmdarray, String[] envp, File dir)
String[] commands = {"C:\\Windows\\System32\\cmd.exe", "/c",
"C:\\MyWork\\Java\\classes\\run.bat"};
File workDir = new File( "C:/MyWork");
Process process = Runtime.getRuntime().exec( commands, null, workDir);
To verify if the batch file is run at all, add a pause
command to the batch file. That will keep the window open so you can verify if the batch file is launched at all, and debug this stage-by-stage.
Upvotes: 0
Reputation: 2311
according to this, the Windows CMD needs the /c
argument, to execute commands like this. try this:
String []commands = {"cmd.exe","/c","C:/MyWork/Java/classes/run.bat"} ;
Upvotes: 1
Reputation: 11921
When using cmd.exe use /C
-Parameter to pass command:
String []commands = {"cmd.exe","/C","C:/MyWork/Java/classes/run.bat"} ;
Upvotes: 2