title: Is it possible to Kick off a java process under Windows Service with C#?
body: <p>I would like to wrap a java program into a windows service with C# using <code>System.ServiceProcess.ServiceBase</code>. So I came up with the following code:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// The main entry point for the application.
  /// &lt;/summary&gt;
  static void Main()
  {
      System.ServiceProcess.ServiceBase.Run(new JavaLauncher());
  }
  protected override void OnStart(string[] args)
  {
      Thread _thread;
      _thread = new Thread(StartService);
      _thread.Start();
      base.OnStart(args);
  }
  protected override void OnStop()
  {
      Thread _thread;
      _thread = new Thread(StopService);
      _thread.Start();
      base.OnStop();
  }
  static public void StartService()
  {
      System.Diagnostics.Process proc = new System.Diagnostics.Process();
      proc.EnableRaisingEvents = false;
      proc.StartInfo.FileName = "javaw";
      proc.StartInfo.Arguments = config.generateLaunchCommand();
      proc.Start();
  }
  static public void StopService()
  {
      System.Diagnostics.Process proc = new System.Diagnostics.Process();
      proc.EnableRaisingEvents = false;
      proc.StartInfo.FileName = "javaw";
      proc.StartInfo.Arguments = "-jar stop.jar";
      proc.Start();
  }
  </code></pre>
  
  <p>Firstly I had to use Threads in OnStart and OnStop. If not, an exception occurs complaining that the service is terminated because of doing nothing.</p>
  
  <p>Secondly, the service can be hooked up to windows smoothly. However, the service terminates a short while after it is started. I looked into the process monitor, only the service process stays alive for that short while, the <em>javaw</em> process never showed up, however. Is there anyone who knows how this can be fixed?</p>
  
  <p>It works fine in an ordinary console environment. I think it has something to do with Windows service.</p>