Ravi Kumar
Ravi Kumar

Reputation: 39

Unable to close cmd console automatically after excution from java

I'm creating batch file using java and Running the same.. It creates successfully and runs as well.. But after execution , the window cmd prompt doesn't close .. And if i using taskkill /f /im cmd.exe it doesn't allow the complete execution.

How to close the cmd once the execution is completed..?? Any Help would be appreciated...

Runtime rt= Runtime.getRuntime();
rt.exec("cmd /c start "+serverPath+"SCA_"+projComp.getModule()+"_"+projComp.getProjectName()+"_"+projComp.getInside().get(i).getRuleSetName()+".bat", null, new File(serverPath+"MES/SERVER/MOS-ACI"));

Upvotes: 2

Views: 1370

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

You can create your bat file like that:

@echo off
cd //my command
exit

So you can execute your bat file like that:

public static void main(String[] args) {
    try {
        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "start Path-to-your-bat-file\\commandBAT.bat");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
    } catch (Exception e) {
        System.out.println("Exception = " + e);
    }
}

Upvotes: 1

Related Questions