【问题标题】:Executing (jar file ) Batch script from c#从c#执行(jar文件)批处理脚本
【发布时间】:2012-04-20 08:15:01
【问题描述】:

我有一个包含 selenium jar 文件的批处理脚本。我想从 C# 执行批处理脚本。我可以知道这是怎么可能的。

example.bat

java -jar "硒罐的路径"

我想从 C# 中执行这个 example.bat。

【问题讨论】:

    标签: c# selenium jar batch-file executable-jar


    【解决方案1】:

    使用Process and ProcessStartInfo

    command 字符串中输入您的java 命令或批处理文件名。

     System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
    
    // The following commands are needed to redirect the standard output.
    // This means that it will be redirected to the Process.StandardOutput StreamReader.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    // Do not create the black window.
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    
    // Get the output into a string
    string result = proc.StandardOutput.ReadToEnd();
    // Display the command output.
    Console.WriteLine(result);
    

    如果你想异步运行它,试试这个:

    /// <summary>
    /// Execute the command Asynchronously.
    /// </summary>
    /// <param name="command">string command.</param>
    public void ExecuteCommandAsync(string command)
    {
       try
       {
        //Asynchronously start the Thread to process the Execute command request.
        Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
        //Make the thread as background thread.
        objThread.IsBackground = true;
        //Set the Priority of the thread.
        objThread.Priority = ThreadPriority.AboveNormal;
        //Start the thread.
        objThread.Start(command);
       }
       catch (ThreadStartException objException)
       {
        // Log the exception
       }
       catch (ThreadAbortException objException)
       {
        // Log the exception
       }
       catch (Exception objException)
       {
        // Log the exception
       }
    }
    

    【讨论】:

    • 但是jar没有运行,命令提示符正在关闭
    • @saisindhu 把 CreateNoWindow = false 告诉我们你看到了什么。
    • @saisindhu 我添加了输出,它应该写在你的控制台中。这也有助于了解会发生什么。
    • @saisindhu 添加了另一种方法,使用不同的线程。
    • @saisindhu /c 告诉 cmd 它必须等待参数。在 /C 之后放置批处理文件的路径以便执行。
    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多