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;
}
}
} |
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;
}
}
}
# 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 |
# 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>" |
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>"
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" |
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
Hello my friend! I want to say that this article is awesome, nice written and come with almost all significant infos. I would like to see more posts like this .
thank you
Very nice article, exactly what I needed.
Hello mates, its impressive paragraph about educationand entirely explained,
keep it up all the time.
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!
wonderful points altogether, you just received a emblem new reader.
What might you suggest about your post that you just
made some days in past times? Any certain?
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.
Paragraph writing is also a excitement, if you know after that you can write
or else it is difficult to write.
Thanks very nice blog!
good tutorial