使用 Process.Start() 时,如何在程序运行期间保持 cmd 提示符打开?
现在我正在尝试使用 C# 打开 .java 服务器或启动同一服务器的 .bat 文件。我可以很好地启动它,但是如果由于我想要执行的单个命令执行而很快关闭。问题是我需要保持命令窗口打开才能保持服务器正常运行。
对于如何启动该过程,我有几个不同的版本,这是发布的最短版本:
Process seleniumServer;
ProcessStartInfo seleniumServerProcessStartInfo = new ProcessStartInfo("java", @"C:\Users\full\path\to\file\selenium-server.jar");
Process.Start(seleniumServerProcessStartInfo);
How can I make I make certain the cmd window that popups that launch the selenium-server.jar file keep open until the program closes?
Right now I'm trying to use C# to open wither a .java server, or a .bat file that launched the same server. I am able to launch it fine, however if quickly closes since the single command I wanted to perform executed. The problem is I need to keep the cmd window open in order to keep the server up.
I have a few different version of how I want to start the process, this being the shortest one to post:
Process seleniumServer;
ProcessStartInfo seleniumServerProcessStartInfo = new ProcessStartInfo("java", @"C:\Users\full\path\to\file\selenium-server.jar");
Process.Start(seleniumServerProcessStartInfo);
How can I make sure the cmd window that popups which launches the selenium-server.jar file keeps open until the program closes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用“-jar foo.jar”作为参数启动它,例如:
窗口可能会消失,因为 jar 永远不会启动。
Launch it with "-jar foo.jar" as args, like:
The window is probably disappearing because the jar never gets launched.
您可以改为运行 cmd /C your-command ,这会保持命令行打开。
You can run
cmd /C your-command
instead, which keeps command line open.使用 Process.WaitForExit。
Use Process.WaitForExit.
可能是 java 进程因错误而停止(它可能对环境变量很挑剔)
Might be that the java process stops with an error (it can be picky on the environment vars)