How to create a windows service from java app

前端 未结 19 1925
既然无缘
既然无缘 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 04:59

    Apache Commons Daemon is a good alternative. It has Procrun for windows services, and Jsvc for unix daemons. It uses less restrictive Apache license, and Apache Tomcat uses it as a part of itself to run on Windows and Linux! To get it work is a bit tricky, but there is an exhaustive article with working example.

    Besides that, you may look at the bin\service.bat in Apache Tomcat to get an idea how to setup the service. In Tomcat they rename the Procrun binaries (prunsrv.exe -> tomcat6.exe, prunmgr.exe -> tomcat6w.exe).

    Something I struggled with using Procrun, your start and stop methods must accept the parameters (String[] argv). For example "start(String[] argv)" and "stop(String[] argv)" would work, but "start()" and "stop()" would cause errors. If you can't modify those calls, consider making a bootstrapper class that can massage those calls to fit your needs.

    0 讨论(0)
  • 2020-11-22 05:02

    I always just use sc.exe (see http://support.microsoft.com/kb/251192). It should be installed on XP from SP1, and if it's not in your flavor of Vista, you can download load it with the Vista resource kit.

    I haven't done anything too complicated with Java, but using either a fully qualified command line argument (x:\java.exe ....) or creating a script with Ant to include depencies and set parameters works fine for me.

    0 讨论(0)
  • 2020-11-22 05:03

    Yet another answer is Yet Another Java Service Wrapper, this seems like a good alternative to Java Service Wrapper as has better licensing. It is also intended to be easy to move from JSW to YAJSW. Certainly for me, brand new to windows servers and trying to get a Java app running as a service, it was very easy to use.

    Some others I found, but didn't end up using:

    • Java Service Launcher I didn't use this because it looked more complicated to get working than YAJSW. I don't think this is a wrapper.
    • JSmooth Creating Window's services isn't its primary goal, but can be done. I didn't use this because there's been no activity since 2007.
    0 讨论(0)
  • 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 <project-name>-install.bat and if you want to uninstall the service run <project-name>-uninstall.bat. To start and stop the service use <project-name>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.

    0 讨论(0)
  • 2020-11-22 05:03

    With Java 8 we can handle this scenario without any external tools. javapackager tool coming with java 8 provides an option to create self contained application bundles:

    -native type Generate self-contained application bundles (if possible). Use the -B option to provide arguments to the bundlers being used. If type is specified, then only a bundle of this type is created. If no type is specified, all is used.

    The following values are valid for type:

    -native type
    Generate self-contained application bundles (if possible). Use the -B option to provide arguments to the bundlers being used. If type is specified, then only a bundle of this type is created. If no type is specified, all is used.
    
    The following values are valid for type:
    
    all: Runs all of the installers for the platform on which it is running, and creates a disk image for the application. This value is used if type is not specified.
    installer: Runs all of the installers for the platform on which it is running.
    image: Creates a disk image for the application. On OS X, the image is the .app file. On Linux, the image is the directory that gets installed.
    dmg: Generates a DMG file for OS X.
    pkg: Generates a .pkg package for OS X.
    mac.appStore: Generates a package for the Mac App Store.
    rpm: Generates an RPM package for Linux.
    deb: Generates a Debian package for Linux.
    

    In case of windows refer the following doc we can create msi or exe as needed.

    exe: Generates a Windows .exe package.
    msi: Generates a Windows Installer package.
    
    0 讨论(0)
  • 2020-11-22 05:04

    JavaService is LGPL. It is very easy and stable. Highly recommended.

    0 讨论(0)
提交回复
热议问题