1

I am invoking the build.bat file using this

rt.exec(new String[]{"cmd.exe","/C","start", "/MIN","build.bat"});

This line opens a command prompt. The build is successful but the window remains open. How do I close it? Tried destroying the process and other System.ext methods. no use

6 Answers 6

2

You shouldn't need to use start /MIN in your arguments. Have you tried this?

rt.exec(new String[]{"cmd.exe","/C","build.bat"});
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this. The build is not getting executed with this option.
Huh, interesting. If that's the case, I suspect it's something with your bat file. START essentially will spawn a new cmd.exe process (which is the one that does not exit) and /C will exit the one you spawned. You might try adding some logging to the batch file and see how far along it's getting to.
1

at the end of bat file add exit ,also no need to add cmd ahead you can directly execute bat

3 Comments

I tried adding exit at the end of the bat file. No help. How do i invoke the bat file without using the 'cmd' option.?
just directly rt.exec(new String[]{"C:\\path\\build.bat"});
@JigarJoshi My batch file doensn't close command prompt window when java application throws exception.I have added exit at the end.
1
writer.write("expdp greenbuds/greenbuds directory=backup dumpfile="+dd+".dump logfile="+dd+".log");
writer.newLine();
writer.write("Exit");

1 Comment

Welcome to Stack Overflow! Would you consider adding some narrative to explain why this code works, and what makes it an answer to the question? This would be very helpful to the person asking the question, and anyone else who comes along.
1

I am just starting out here in SO, so couldn't upvote @Franci Penov's answer as correct:

rt.exec(new String[]{"cmd.exe","/C","build.bat"}); 

or simply,

rt.exec("cmd.exe /C build.bat");

should not bring up a command prompt. build.bat will be executed headlessly.


Suppose you do want to bring up a command prompt while build.bat is running (so you can see the output right from the command prompt instead of processing it later), and want the command prompt to go away after build.bat has finished execution, you may use the following:

rt.exec("cmd.exe /C start cmd.exe /C batch.bat");

the start starts a (visible) command prompt window to process and display the output of cmd.exe /C batch.bat. If you have a pause at the end of your batch.bat then all you need to do is hitting a key, and the command prompt will go away without the need to type exit at the prompt window to close it.

This may not be what you need, but I have been searching on SO but then found out the solution myself. Just to share it.

1 Comment

Thank you so much!! After 20 tabs opened and hours, the second part of your answer is the solution I needed!! Thank you so much!!
0

This is a fix I found on StackOverflow, for closing a command prompt, after executing a program using a batch-file.

Instead of: start "some program path"

do this: start "" "some program path"

Works for me under Windows XP.

Comments

-1

Just update your batch (.bat) file with a new line and type in that new line : exit.

1 Comment

Already suggested. Read previous answers and comments before posting.

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.