开发者

Running a .bat/ .cmd file from Java [closed]

开发者 https://www.devze.com 2023-01-08 07:08 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

I wanted to run a .cmd file from Java. I have something which works for me. Can someone help me understand possible failures of my program.

import java.io.IOException;  

/* 
  How to run a batch .bat or .cmd file from Java? 
  1. I don't want the command window to open up. It should be in background. 
  2. Gracefully destroy any new process created. 
  3. Need to confirm the quality of the program with experts. 
 */  
public class Run开发者_运维百科Bat {  
    public static void main(String args[]) {  
        Runtime run = Runtime.getRuntime();  
        //The best possible I found is to construct a command which you want to execute  
        //as a string and use that in exec. If the batch file takes command line arguments  
        //the command can be constructed a array of strings and pass the array as input to  
        //the exec method. The command can also be passed externally as input to the method.  

        Process p = null;  
        String cmd = "D:\\Database\\TableToCSV.cmd";  
        try {  
            p = run.exec(cmd);  
            p.getErrorStream();  
            System.out.println("RUN.COMPLETED.SUCCESSFULLY");  
        }  
        catch (IOException e) {  
            e.printStackTrace();  
            System.out.println("ERROR.RUNNING.CMD");  
            p.destroy();  
        }  
    }  
}  

Is my solution reliable? How can I make sure that once the .cmd is execute there is no processes hanging around.

Thanks.


I don't know what you are doing with p.getErrorStream(), You are not accesing it.

Way to determine result i.e. exit code of command executed is by adding following lines after

p = run.exec(cmd);
p.waitFor();
System.out.println(p.exitValue());

And put p.destroy() in finally block.

Hope this helps.


Execute your command as:

cmd.exe /C d:\database\tabletoCSV.cmd

See cmd.exe /? for more information:

> cmd /?
Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
   [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
[...]


Like Carl just mentioned

  • You are not capturing any output error / success output.
  • Your are not making the process thread wait for exitValue.
  • Have you given a look at ProcessBuilder class?

Anyway , you can have a look at following code

    Process proc = null;
    Runtime rt = Runtime.getRuntime();
    try {
        proc = rt.exec(cmd);
        InputStream outCmdStream = proc.getInputStream();
        InputStreamReader outCmdReader = new InputStreamReader(outCmdStream);
        BufferedReader outCmdBufReader = new BufferedReader(outCmdReader);
        String outLine;
        while ((outLine = outCmdBufReader.readLine()) != null) {
            System.out.println(outLine);
        }
        InputStream errStream = proc.getErrorStream();
        InputStreamReader errReader = new InputStreamReader(errStream);
        BufferedReader errBufReader = new BufferedReader(errReader);
        String errLine;
        while ((errLine = errBufReader.readLine()) != null) {
            System.out.println(errLine);
        }
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("ERROR.RUNNING.CMD");
        proc.destroy();
    }
}

Hope this helps


There's another thing wrong with this code that other answers don't indicate: If the process you start generates (console) output and you don't connect up its output stream, it will stall and fail for no apparent reason. For some programs and environments I've found it necessary to connect up separate threads to keep both the output and error streams drained. And to capture their output so you're not flying blind.

If you have a modern Java (post 1.5), you could also be looking at the ProcessBuilder class as a means to start up external programs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号