Create Windows Service Application with JAVA (procrun)

Alhamdulillah, after using one full days to create windows service in java, I already made it based on tutorial at here. So, I just want to create another my own tutorial, so I hope it can help you to create windows service in java with procrun wrapper.

I used :

  • jdk-7u4-windows-x64 (windows 7 64)
  • IDE netbeans 7
  • Log4j 1.2.17
  • Procrun

To create windows service in java wit procrun :

  • my windows service source code
package sample.windows.service;
 
import org.apache.log4j.Logger;
 
/**
*
* @author arif
*/
public class Driver {
 
private static Logger log = Logger.getLogger("windowsservicelog");
private static boolean stop = false;
 
public static void start(String[] args) {
while (!stop) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
log.info("running");
}
}
 
public static void stop(String[] args) {
log.info("stop");
stop = true;
}
 
static void main(String args[]) {
String mode = args[0];
switch (mode) {
case "start":
start(args);
break;
case "stop":
stop(args);
break;
}
}
}
  • my log4j configuration
# Root logger option
log4j.rootLogger=debug, file, stdout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\windowsservice.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  • simple template procrun script
procrun.exe //IS//<your service name> --Install="<your procrun path>procrun.exe" --Jvm=auto --Startup=auto
--StartMode jvm --StartClass <your start class> --StartParams start
--StopMode jvm --StopClass <your stop class> --StopParams stop
--Classpath="<your service jar path><your service jar>.jar" --DisplayName="<your display service name>"
  • my procrun script
procrun.exe //IS//SampleService --Install="D:sample_windows_serviceprocrun.exe" --Jvm=auto --Startup=auto
--StartMode jvm --StartClass sample.windows.service.Driver --StartParams start
--StopMode jvm --StopClass sample.windows.service.Driver --StopParams stop
--Classpath="D:sample_windows_servicejava_windows_service.jar" --DisplayName="Sample Service JAVA"

complete tutorial, project and my service folder

11 thoughts on “Create Windows Service Application with JAVA (procrun)

  1. This is the right website for anyone who hopes to find out about this topic.
    You know so much its almost tough to argue with you (not that I really would
    want toHaHa). You certainly put a fresh spin on a topic that’s been discussed for years.
    Excellent stuff, just great!

  2. Just wish to say your article is as astonishing.

    The clearness in your post is simply nice and i can assume you are an expert on this
    subject. Well with your permission let me to grab your RSS feed to
    keep up to date with forthcoming post. Thanks a
    million and please continue the gratifying work.

  3. Pingback: Google

Leave a Reply

Your email address will not be published. Required fields are marked *

Afiseaza emoticoanele Locco.Ro