How to create a windows service from java app

前端 未结 19 2302
既然无缘
既然无缘 2020-11-22 04:09

I\'ve just inherited a java application that needs to be installed as a service on XP and vista. It\'s been about 8 years since I\'ve used windows in any form and I\'ve neve

19条回答
  •  一个人的身影
    2020-11-22 05:03

    If you use Gradle Build Tool you can try my windows-service-plugin, which facilitates using of Apache Commons Daemon Procrun.

    To create a java windows service application with the plugin you need to go through several simple steps.

    1. Create a main service class with the appropriate method.

      public class MyService {
      
          public static void main(String[] args) {
              String command = "start";
              if (args.length > 0) {
                  command = args[0];
              }
              if ("start".equals(command)) {
                  // process service start function
              } else {
                  // process service stop function
              }
          }
      
      }
      
    2. Include the plugin into your build.gradle file.

      buildscript {
        repositories {
          maven {
            url "https://plugins.gradle.org/m2/"
          }
        }
        dependencies {
          classpath "gradle.plugin.com.github.alexeylisyutenko:windows-service-plugin:1.1.0"
        }
      }
      
      apply plugin: "com.github.alexeylisyutenko.windows-service-plugin"
      

      The same script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:

      plugins {
        id "com.github.alexeylisyutenko.windows-service-plugin" version "1.1.0"
      }
      
    3. Configure the plugin.

      windowsService {
        architecture = 'amd64'
        displayName = 'TestService'
        description = 'Service generated with using gradle plugin'   
        startClass = 'MyService'
        startMethod = 'main'
        startParams = 'start'
        stopClass = 'MyService'
        stopMethod = 'main'
        stopParams = 'stop'
        startup = 'auto'
      }
      
    4. Run createWindowsService gradle task to create a windows service distribution.

    That's all you need to do to create a simple windows service. The plugin will automatically download Apache Commons Daemon Procrun binaries, extract this binaries to the service distribution directory and create batch files for installation/uninstallation of the service.

    In ${project.buildDir}/windows-service directory you will find service executables, batch scripts for installation/uninstallation of the service and all runtime libraries. To install the service run -install.bat and if you want to uninstall the service run -uninstall.bat. To start and stop the service use w.exe executable.

    Note that the method handling service start should create and start a separate thread to carry out the processing, and then return. The main method is called from different threads when you start and stop the service.

    For more information, please read about the plugin and Apache Commons Daemon Procrun.

提交回复
热议问题