简体   繁体   English

如何停止关闭命令提示符

[英]How to Stop Closing Command Prompt

Suppose If we create a bat file to run java program which prints "Hello World" , like this 假设如果我们创建一个bat文件来运行打印"Hello World" java程序,就像这样

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. 当我双击该bat文件后,它将打开命令提示符并显示"Hello World"结果并自动关闭。 Is there any solution to not to close Command prompt until and unless I type Exit in it. 有什么解决方案可以不关闭命令提示符,直到并且除非我在其中键入Exit。

Thanks. 谢谢。

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 Prompt window (fire up a new one, navigate to your working directory, execute it) 在“ Command Prompt窗口中运行该命令javac MyProgram.java java MyProgram (启动一个新窗口,导航至您的工作目录,然后执行它)
  • 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. 在程序结尾获取一个字符( Scanner a; ... ; a.nextLine() ),然后可以输入内容或直接按a即可完成程序。
  • Make the current thread sleep for some time so that you can see the output. 使当前线程休眠一段时间,以便可以看到输出。 Try: try { Thread.Sleep(1000); } catch (Exception e) {} 试试: try { Thread.Sleep(1000); } catch (Exception e) {} try { Thread.Sleep(1000); } catch (Exception e) {}

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. 您可以手动打开命令提示符,然后将.bat文件拖放到此命令提示符并运行它。 This way, you would still have the window open. 这样,您仍然可以打开窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM