如何从Windows服务运行jar文件

时间:2012-10-10 03:24:35

标签: c# java windows jar windows-services

我已经编写了一个WPF应用程序,我可以在其中输入我的dos命令,并使用C#获取一个带有响应的消息框,并且它工作得很好。现在我想编写一个可以间隔执行命令“ java -jar myfile.jar ”的服务。 jar文件正在访问weblogic队列并返回由下面的函数读取的结果。问题是服务不起作用,尽管下面的功能在世界粮食计划署成功运作。

如何在Windows服务中使用此功能?

    /// <summary>
    /// Executes a shell command synchronously.
    /// </summary>
    /// <param name="command">string command</param>
    /// <returns>string, as output of the command.</returns>
    public void ExecuteCommandSync(object command)
    {
     try
     {
         // create the ProcessStartInfo using "cmd" as the program to be run,
         // and "/c " as the parameters.
         // Incidentally, /c tells cmd that we want it to execute the command that follows,
         // and then exit.
    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);
      }
      catch (Exception objException)
      {
      // Log the exception
      }
}

codeproject.com

获取的代码

0 个答案:

没有答案