Il est toujours utile de démarrer un programme Java en service Windows : il n'y a pas besoin de se connecter pour démarrer le programme, En cas de plantage Windows sait le redémarrer, Le bureau n'est pas pollué avec des consoles dans tous les sens, etc. Comment es-t il possible de créer un service à partir d'un code Java, c'est ce que je vais vous présenter dans cette section.
Le démarrage d'un programme Java par un service Windows (NT, 2000 ou SP) se fait sur le principe d'appels à des bibliothèques système. Pou avoir plus d'informations sur l'appel de bibliothèques système, rapportez-vous à l'article.
L'ensemble de cet article repose sur l'excellent article de Tanuki Software qui propose en open source un download (src). Si le lien ne fonctionne plus, voici les fichiers (bin ou src) depuis mon site. Je ne vais pas m'acharner à reprendre les docs qui ont été déjà très bien réalisée, mais je vais résumer mon utilisation avec un exemple simple : Transformer en service Windows le déclenchement d'un programme Java lors de l'arrivée d'un mail sur un compte.
#******************************************************************** # Wrapper Properties #******************************************************************** # Java Application wrapper.java.command=java # Java Main class. This class must implement the WrapperListener interface # or guarantee that the WrapperManager class is initialized. Helper # classes are provided to do this for you. See the Integration section # of the documentation for details. wrapper.java.mainclass=com.penpen.portail.mail.ServiceXP # Java Classpath (include wrapper.jar) Add class path elements as # needed starting from 1 wrapper.java.classpath.1=../lib/wrapper.jar wrapper.java.classpath.2=../lib/BureauMail.jar wrapper.java.classpath.3=../lib/swt.jar wrapper.java.classpath.4=../conf wrapper.java.classpath.5=../lib/rt.jar wrapper.java.classpath.6=../lib/commons-net-1.2.2.jar # Java Library Path (location of Wrapper.DLL or libwrapper.so) wrapper.java.library.path.1=../lib # Java Additional Parameters wrapper.java.additional.1=-Dprogram.name=setup.bat # Initial Java Heap Size (in MB) wrapper.java.initmemory=3 # Maximum Java Heap Size (in MB) wrapper.java.maxmemory=64 # Application parameters. Add parameters as needed starting from 1 #wrapper.app.parameter.1= #******************************************************************** # Wrapper Logging Properties #******************************************************************** # Format of output for the console. (See docs for formats) wrapper.console.format=PM # Log Level for console output. (See docs for log levels) wrapper.console.loglevel=INFO # Log file to use for wrapper output logging. wrapper.logfile=../logs/wrapper.log # Format of output for the log file. (See docs for formats) wrapper.logfile.format=LPTM # Log Level for log file output. (See docs for log levels) wrapper.logfile.loglevel=INFO # Maximum size that the log file will be allowed to grow to before # the log is rolled. Size is specified in bytes. The default value # of 0, disables log rolling. May abbreviate with the 'k' (kb) or # 'm' (mb) suffix. For example: 10m = 10 megabytes. wrapper.logfile.maxsize=0 # Maximum number of rolled log files which will be allowed before old # files are deleted. The default value of 0 implies no limit. wrapper.logfile.maxfiles=0 # Log Level for sys/event log output. (See docs for log levels) wrapper.syslog.loglevel=NONE #******************************************************************** # Wrapper NT Service Properties #******************************************************************** # WARNING - Do not modify any of these properties when an application # using this configuration file has been installed as a service. # Please uninstall the service before modifying this section. The # service can then be reinstalled. # Name of the service wrapper.ntservice.name=BureauMail # Display name of the service wrapper.ntservice.displayname=Bureau Mail PenPen(C) # Description of the service wrapper.ntservice.description=Service d'administration du Bureau Web. # Service dependencies. Add dependencies as needed starting from 1 wrapper.ntservice.dependency.1= # Mode in which the service is installed. AUTO_START or DEMAND_START wrapper.ntservice.starttype=AUTO # Allow the service to interact with the desktop. wrapper.ntservice.interactive=true |