使用 Process.Start() 时,如何在程序运行期间保持 cmd 提示符打开?

发布于 2024-10-19 15:37:58 字数 520 浏览 0 评论 0原文

现在我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒几许 2024-10-26 15:37:58

使用“-jar foo.jar”作为参数启动它,例如:

ProcessStartInfo seleniumServerProcessStartInfo = new ProcessStartInfo("java", @"-jar C:\Users\full\path\to\file\selenium-server.jar");

窗口可能会消失,因为 jar 永远不会启动。

Launch it with "-jar foo.jar" as args, like:

ProcessStartInfo seleniumServerProcessStartInfo = new ProcessStartInfo("java", @"-jar C:\Users\full\path\to\file\selenium-server.jar");

The window is probably disappearing because the jar never gets launched.

自此以后,行同陌路 2024-10-26 15:37:58

您可以改为运行 cmd /C your-command ,这会保持命令行打开。

You can run cmd /C your-command instead, which keeps command line open.

放赐 2024-10-26 15:37:58

可能是 java 进程因错误而停止(它可能对环境变量很挑剔)

    Process selenium = Process.Start(seleniumServerProcessStartInfo); 

    selenium.WaitForExit();

    int exit = selenium.ExitCode;

    if (exit!=0)
       throw new Exception("java selenium server stopped with error");

Might be that the java process stops with an error (it can be picky on the environment vars)

    Process selenium = Process.Start(seleniumServerProcessStartInfo); 

    selenium.WaitForExit();

    int exit = selenium.ExitCode;

    if (exit!=0)
       throw new Exception("java selenium server stopped with error");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文