I want to explain by giving a small demo to run a java class as window service.
Step 1: Download srvany.exe and instsrv.exe from this link.
Step 2: Go to DOS prompt and type the command like
c:\ instsrv.exe srvany.exe
Above command will create a service with the given name in <> brackets.
e.g. c:\srvany\instsrv.exe Testing c:\srvany\srvany.exe
This will create a service with the name “Testing” ,we can check this by opening the services.msc from from control pannel.
Step 3: Now I am making a folder “Testing” in c:\ where I am putting my java application which will run as a service.
I am writing a simple program which will write a text file after 5000 ms on the same location.
Source : TestService.java
import java.io.*;
public class TestService {
public static void main(String[] args) {
int i=0;
while(true) {
System.out.println("------------------------------");
System.out.println("Service Start...");
System.out.println("------------------------------");
try {
writeToFile("Services_Test_"+i+".txt");
i++;
Thread.sleep(5000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
writeToFile("Error");
}
}
}
public static void writeToFile(String filename) {
BufferedWriter bufferedWriter = null;
try {
//Construct the BufferedWriter object
bufferedWriter = new BufferedWriter(new FileWriter(filename));
//Start writing to the output stream
bufferedWriter.write("Writing line one to file");
bufferedWriter.newLine();
bufferedWriter.write("Writing line two to file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Step 4: now we need to configure this java application into windows registry for creating it as a windows nt service.
In step 1 we create a service with the name ”Testing” by using srvany.exe and instsrv.exe
Type regedit in run it will open registry.
Lookup our service in this path in registry window
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Testing
Step 5: Now right click on Testing and add a new key with the name as “Parameters”.
Step 6 : Now click on Parameters then it will show a Defalut value in right side window .right click on right side window and then select New-> String Value .
Create three String value
1. Application
2. AppParameters
3. AppDirectory
See in the screenshot below.Step 7 : Click on Application and give the java.exe path of your local machine.
Step 8 : click on AppParameters and give the java class or jar name which we want to run as a service.
Step 9: Click on AppDirectory and give the path of the directory where you put your java application in the system.
Step 10 : Now service is ready to run, we can run and stop this service from services.msc.
Step 10 : Check the output this service will generates the text file in the current location where we put class file. With same manner we can run a jar file as a service.
Enjoy your service :)