Jaya Santosh
Jaya Santosh

Reputation: 1

How to Stop Closing Command Prompt

Suppose If we create a bat file to run java program which prints "Hello World" , like this

javac MyProgram.java java MyProgram

After when I Double click that bat file It opens Command prompt and displays "Hello World" result and automatically closes. Is there any solution to not to close Command prompt until and unless I type Exit in it.

Thanks.

Upvotes: 1

Views: 688

Answers (2)

KireinaHoro
KireinaHoro

Reputation: 585

If you want to make the window remain for a while, there are 3 ways:

  • Run that command javac MyProgram.java java MyProgram in a Command Promptwindow (fire up a new one, navigate to your working directory, execute it)
  • Get a char (Scanner a; ... ; a.nextLine()) at the end of your program, you can then enter something or simply press a to make the program complete.
  • Make the current thread sleep for some time so that you can see the output. Try: try { Thread.Sleep(1000); } catch (Exception e) {}

Upvotes: 1

Pavan Dittakavi
Pavan Dittakavi

Reputation: 3181

It happens because at the end of the day it is a program. And it terminates once it is done with its execution..in your case, printing stuff.

You can manually open a command prompt and then drag and drop the .bat file onto this command prompt and run it. This way, you would still have the window open.

Upvotes: 0

Related Questions