Skip to main content

Register a Java program as a Windows service

Mr.ChenAbout 1 minarticleJavaWindows

Operating environment

The computer should have a java runtime environment.

Requires software

Java Service Wrapper

Download link: http://sourceforge.net/projects/wrapper/open in new window

configuration process

  1. First, package the java program into a jar package.

  2. Decompress the Java Service Wrapper package.

  3. For example, if the program project directory to be registered as a service is workapp, then create three folders under this directory: bin, conf, lib.

  4. Copy Wrapper.exe in the bin directory of the Java Service Wrapper package, App.bat.inopen in new window, InstallApp-NT.bat.inopen in new window, UninstallApp-NT.bat.inopen in new window files in the src/bin directory to the bin directory of the workapp , and renamed to App.bat, InstallApp-NT.bat, UninstallApp-NT.bat respectively.

  5. Copy Wrapper.DLL and wrapper.jar in the lib directory of the Java Service Wrapper package to the lib directory of the workapp. And copy the jar of the project and the jars used to this directory.

  6. Copy the wrapper.conf.inopen in new window in the srcconf directory of the Java Service Wrapper package to the conf directory of the workapp, and name it wrapper.conf.

  7. Modify the wrapper.conf configuration file, as follows:

    Mainly modify the following items:

    • JVM location:

      wrapper.java.command=C:\jdk1.5.0_07\bin\java
      
    • Operating parameters: such as:

      wrapper.java.additional.1=-Dprogram.name=run.bat
      
    • All jar packages required by your Java program must be marked here, pay attention to the correct path:

      wrapper.java.classpath.1=../lib/wrapper.jar
      wrapper.java.classpath.2=../lib/test_fat.jar
      ...
      
    • The directory where your Wrapper.DLL or wrapper.jar is located

      wrapper.java.library.path.1=../lib
      
    • MAIN CLASS This determines the way to use the Java Service Wrapper

      wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
      
    • The runtime class of your Java application

      wrapper.app.parameter.1=com.tes.Testjar
      
    • Register as the name and display name of the service, you can set it as you like

      wrapper.ntservice.name=testService
      wrapper.ntservice.displayname=testService
      
    • service description information

      wrapper.ntservice.description = testService
      
  8. After the modification is completed, run MyApp.bat to run your java program. Here you can test whether the configuration is correct. If it can run, it proves that the configuration is ok.

  9. Run InstallApp-NT.bat to register the service, and UninstallApp-NT.bat to cancel the service.

  10. After running the registration service InstallApp-NT.bat, you can see the service name you registered in Control Panel - Management Program - Service. (eg: testService)