28
======================================================== JAVA Application Registration as Windows Service ======================================================== This tutorial is meant for registering any java application as a windows service using some easy tools and techniques. Introduction:- First, let me start with the introduction of this “Java application registration as windows service”. Whenever we want to use a java application as a windows service facility to be used on Operating System start up we can, for example we are making any Socket programming application and we want to provide server facility at the start up time so we will register that “Server.java” file which contains the information for the ip address and the port no. so whenever we are running any “Client.java” file so it will able to communicate using that service. So this way we will have an advantage of this idea. Let me ask you that have you ever faced to turn any java class as windows service? if yes, then here you can find the complete solution for it in just some easy steps. How to register any java application as windows service? There are so many ways we can register java application as windows service and so many tools available over the internet. But I found this tool as easy as in the simple way we can register any java application as windows service. The tool name is “Java Service Wrapper ” tool which we will use for our understanding purpose and for our solution. I have done more R&D on this topic and found this tool from open source Java Service Wrapper project from Tanukisoftware.org (http://wrapper.tanukisoftware.org/doc/english/index.html ).

JAVA Application Registration as Windows Service

Embed Size (px)

DESCRIPTION

There are numerous to complete this work. By far, the top reserve is to run your java class as a window service. One of the simplest tool’ have search for turning classes into services is the open source java service.

Citation preview

Page 1: JAVA Application Registration as Windows Service

======================================================== JAVA Application Registration as Windows Service

========================================================

This tutorial is meant for registering any java application as a windows service using some easy tools and techniques.

Introduction:-

First, let me start with the introduction of this “Java application registration as windows service”.

Whenever we want to use a java application as a windows service facility to be used on Operating System start up we can, for example we are making any Socket programming application and we want to provide server facility at the start up time so we will register that “Server.java” file which contains the information for the ip address and the port no. so whenever we are running any “Client.java” file so it will able to communicate using that service. So this way we will have an advantage of this idea.

Let me ask you that have you ever faced to turn any java class as windows service? if yes, then here you can find the complete solution for it in just some easy steps.

How to register any java application as windows service?

There are so many ways we can register java application as windows service and so many tools available over the internet. But I found this tool as easy as in the simple way we can register any java application as windows service.

The tool name is “Java Service Wrapper” tool which we will use for our understanding purpose and for our solution.

I have done more R&D on this topic and found this tool from open source Java Service Wrapper project from Tanukisoftware.org (http://wrapper.tanukisoftware.org/doc/english/index.html).

According to their website, the Java Service Wrapper is an application which has evolved out of a desire to solve a number of problems common to many Java applications such as,

Run java application as a Windows Service Application Reliability On Demand Restarts Flexible Configuration Ease Application Installations Logging

In this tutorial we will focus on how to register java application as windows service,

Steps for running java application as windows service:

Page 2: JAVA Application Registration as Windows Service

1: Download the tool named (Java Service Wrapper-community edition) from the given below link,

http://wrapper.tanukisoftware.com/doc/english/download.jsp#stable

(Note: Please download the Community Edition only)

I have used below mentioned version of java service wrapper tool.

http://wrapper.tanukisoftware.com/download/3.5.17/wrapper-windows-x86-32-3.5.17.zip

There is total four integration method available of this tool and I have used method 1.

Integration Method 1 (WrapperSimpleApp) Integration Method 2 (WrapperStartStopApp) Integration Method 3 (WrapperListener) Integration Method 4 (WrapperJarApp)

The Method 1 is to use the WrapperSimpleApp helper class to launch the application.

When integrating with this Method 1 (WrapperSimpleApp helper class), the WrapperSimpleApp class replaces an application's main class. This gives the WrapperSimpleApp class a chance to immediately initialize theWrapperManager and register the JVM with the Wrapper. The WrapperSimpleApp class then manages all interaction with the Wrapper as well as the life-cycle of an application. When the Wrapper sends a start message to the JVM via the WrapperManager, the main method of the application's actual main class is called.

The WrapperSimpleApp helper class is told how to launch the application by passing the application's main class name, followed by any additional application parameters to the main method of the WrapperSimpleApp.

2: Now extract the downloaded zip community edition into “C” Drive,e.g. (C:\wrapper-windows-x86-32-3.5.17), you can extract it in any drive, as I have extracted it in “C” drive so that I can easily access path with specific folder, the structure of this tool “wrapper-windows-x86-32-3.5.17” will be as shown below,

Page 3: JAVA Application Registration as Windows Service

3: Check lib folder inside the extracted folder with name “wrapper-windows-x86-32-3.5.17” which contains the "wrapper.dll or wrapper-windows-x86-32.dll" file and "wrapper.jar" and "wrappertest.jar".

4: Check wrapper configuration file named "wrapper.conf" on below mentioned path

C:\wrapper-windows-x86-32-3.5.17\conf -> “ wrapper.conf ”

5: Now find this path -> C:\wrapper-windows-x86-32-3.5.17\src\bin

6: Here renamed these three files as mentioned below,

Original, ("App.bat.in", "InstallApp-NT.bat.in", "UninstallApp-NT.bat.in") Replaced With, Apply this change ("App.bat", "InstallApp-NT.bat", "UninstallApp-NT.bat").

7: Just copy & paste these renamed three files on this Path -> C:\wrapper-windows-x86-32-3.5.17\bin

8: Check that this file ("wrapper.exe" or "wrapper-windows-x86-32.exe") is Present in below path or not, -> C:\wrapper-windows-x86-32-3.5.17\bin

9: Create jar file of that java application which you want to run or register as windows service.

Page 4: JAVA Application Registration as Windows Service

Follow this for more information that how to create jar file of your application so we can use that jar file as windows service by registering it.

Now we will create jar file of this client server application as mentioned in the next screen,

Page 5: JAVA Application Registration as Windows Service
Page 6: JAVA Application Registration as Windows Service
Page 7: JAVA Application Registration as Windows Service
Page 8: JAVA Application Registration as Windows Service

And click on the finish and check on the path where you have created jar file of this application.10: Now Copy this created jar and put on this

Path -> C:\wrapper-windows-x86-32-3.5.17\lib.

11: Let’s test your created jar file to make sure that everything is ok. At a command prompt, Use cd command to go in particular directory,

Page 9: JAVA Application Registration as Windows Service

Just go to that directory in “C” drive on this,

Path -> C:\wrapper-windows-x86-32-3.5.17\lib

You have pasted the created jar file of your java application here so follow as shown in below screen.

e.g. -> here in my case jar file name is ClientServerService.jar

(service is my package name and Server is my java class name).

Now type the following command as shown in the below screen & verify the result.

Page 10: JAVA Application Registration as Windows Service

C:\wrapper-windows-x86-32-3.5.15\lib> java -cp jar name “ClientServerService.jar” then your package name.your class name ->“service.Server”.12: Check your java application output here.

As you can see in above screen my java application is working properly so I can follow further instruction.

13: If it is your desired output then follow the step No-14 or if you got an exception So check your java application again and make jar of your java application again And follow the steps from step No-9,

14: If you get your desired output then apply below changes in “wrapper.conf” on below path -> C:\wrapper-windows-x86-32-3.5.17\conf as shown in below screen,

Page 11: JAVA Application Registration as Windows Service

Change

wrapper.java.mainclass=org.tanukisoftware.wrapper.test.Main

To

wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp

Add

wrapper.java.classpath.3=../lib/ClientServerService.jar

Below

wrapper.java.classpath.1=../lib/wrapper.jarwrapper.java.classpath.2=../lib/wrappertest.jar

Note: We can also set more class paths e.g. “wrapper.java.classpath.3” here we will have to follow just sequence only e.g. wrapper.java.classpath.3, wrapper.java.classpath.4 and so on, just remove the hash sign from this property and use it.

Page 12: JAVA Application Registration as Windows Service

Change

#wrapper.app.parameter.1=

To

wrapper.app.parameter.1=service.Server#wrapper.app.parameter.2=6066

Note: [We can also set more parameters as like command line arguments as shown.]

Change

And if you do not find these properties then just copy directly "To" content:

[email protected]@[email protected]@[email protected]@

To

Note: (Server is my java class name)

(Paste this below content directly into the “wrapper.conf” file.)

wrapper.ntservice.name= Serverwrapper.ntservice.displayname= Serverwrapper.ntservice.description= Server Test

and save the file.

15: Now let’s test your own creation. At the command prompt as shown in below screen,

%wrapper home%/bin/App where (%wrapper home%) stands for your wrapper home directory.

Page 13: JAVA Application Registration as Windows Service

e.g. My wrapper extracted directory is -> C:\wrapper-windows-x86-32-3.5.17

So type below command and execute from command prompt as shown in above screen and please wait for your output and verify it,

e.g. -> C:\wrapper-windows-x86-32-3.5.15\bin>App.bat 16: Check the result is your java application executed successfully here?

If yes then follow further steps otherwise check the previous steps.

17: Now that we’ve tested our java application class, let’s turn it into a fully fledged windows service. Change this property as per the below screen,

wrapper.displayname=Server Application wrapper.description=Server Application Description

Page 14: JAVA Application Registration as Windows Service

Now, at the command prompt run the below command as shown in below screen:

Where (%wrapper home%) stands for your wrapper extracted home directory.

18: %wrapper home%/bin/InstallApp-NT.bat

%wrapper home% -> stands for your wrapper extracted directory.

e.g. -> C:\wrapper-windows-x86-32-3.5.17\bin>InstallApp-NT.bat

Page 15: JAVA Application Registration as Windows Service

19: If there is a message in command prompt like "Server Application service installed." As shown in below screen then,

-> Congratulation now your java application is successfully converted or registered as windows service.

Page 16: JAVA Application Registration as Windows Service

20: For the verification of your generated service follow these steps as below,

Let’s make sure that Windows recognizes the Server class as a Service. Click on the “services” icon, found in the administrative tools option Of the control panel or write “services” in run so you will see all services list as shown In below screen.

21: You can start/stop your window service and test it from here as per your usage as I have created Server application so we will start this service by clicking on “start” On the left side so I can connect my client with that server as per next screen,

As you can see in below screen that I have click on the start and my registered Server application is going to be start,

Page 17: JAVA Application Registration as Windows Service

As you are able to see that my registered Server application is ready to use so now I can run my Client Application as shown in below screen and will connect that client with Server for client-server simple java application.

Page 18: JAVA Application Registration as Windows Service

As I have run my “Client.java” file I will connect with the same port which I have used in the “Server.java” file so I have connected my client application as per below screen,

Page 19: JAVA Application Registration as Windows Service

Now client can communicate with server by sending a message as shown in below screen,

Page 20: JAVA Application Registration as Windows Service

As my “Client” java program is now connected on port no “6066” as our registered service is working for client-server application because we have already started that Server Application so if we will stop that service from the services window so server will not allow to client for communication.

Page 21: JAVA Application Registration as Windows Service

As we have stopped our registered Server Application’s services so client will not be able to communicate as shown below,

22: If we want to uninstall this registered Server Application so we can also uninstall or completely remove this service using below command as shown in next screen,

But before uninstall that registered application I would like to recommend to you to stop that registered service if it is started for the safe removal support.

e.g. -> C:\wrapper-delta-pack-3.5.17\bin>UninstallApp-NT.bat

Page 22: JAVA Application Registration as Windows Service

Please wait until you get the message shown at command prompt in below screen,

Message is -> “Server Application service removed.”

After you have successfully uninstalled your registered java application you will not able to

Page 23: JAVA Application Registration as Windows Service

see that service in the services list as you can see in the below screen,

So this way I have found this easy “Java Service Wrapper” tool helpful to me for registering my java application as a windows service I hope this tutorial will help you too.

Thank you,

Have a happy coding.