3

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 .

9
  • 1
    How do you start your program? When running it using java.exe, do you see some output from the bat-file when it is executed. Commented Nov 5, 2013 at 10:08
  • I get some time FileNotFoundException when I double click .bat file if the particular .xml file does not found. With the same scenario when I run through java file, I dont get any exception or anything. I dont know whether classname specified inside the .bat file run successfully or not. Commented Nov 5, 2013 at 10:13
  • Is there any console-output from your above code?? Try adding some System.out.printlns to make sure alle statements are executed Commented Nov 5, 2013 at 10:20
  • I have added one "System.out.println("Done!!!");" But it does not get executed and also the while loop is not getting exited. The program never terminates. But if I remove the while loop, it gets terminated with the "Done!!!" message inthe console. Commented Nov 5, 2013 at 10:26
  • @EnglishLearner You must first try your *.bat from the comand line. If that works, run your Java program from the command line. No clicky, clacky in some IDE or windows explorer, you're wasting your time. Commented Nov 5, 2013 at 11:07

4 Answers 4

2

When using cmd.exe use /C-Parameter to pass command:

String []commands = {"cmd.exe","/C","C:/MyWork/Java/classes/run.bat"} ;
Sign up to request clarification or add additional context in comments.

Comments

1

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"} ;

Comments

0

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.

11 Comments

I had added pause (pause > nul)command to the .bat file. But it does not popup the .cmd. I suspect commands which I have written does not execute. Btw how to specify workding directory?"C:/MyWork/Java/classes/run.bat" Is this not a workding directory? can you please explain?
See my edit, forward-slashes are not Windows/MS-DOS path syntax and I think those may be the biggest problem. @EnglishLearner
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 when I call, I can see the contents only not.
1.) My version of cmd.exe (Windows 7) does accept forward-slashes 2.) You have to escape backslashes in java String-literals ("\\")
Backslash needs to be escaped, \\ -- good point @piet.t -- but I'm running Win 7, just tested it & it fails to understand a forward-slash in the cmd /c path\batchfile invocation.
|
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.