tanlccc
tanlccc

Reputation: 373

Using JavaService to add Windows Service

I have created a Java application (runs infinite loop) (with many Java class files) that also uses log4j.jar and `snmp4j.jar? . In order to run the application, I need to provide some input arguments as follows:

C:\APP>java myApp.class arg1 arg2 arg3 arg4 arg5 arg6

where myApp.class contains the main handler.

This application works fine under command line execution. However, I want to create a Windows service for this application so that it will run whenever the PC is started. Also, if the service is down for whatever reason, it will get re-started under Windows Services tool.

I tried to use JavaService for my purpose, by executing the following command:

C:\APP>JavaService.exe -install MyService "C:\java\jdk1.6.0.29\jre\bin\server\jvm.dll" -start myApp.class -params arg1 arg2 arg3 arg4 arg5 arg6
The MyService automatic service was successfully installed

I can see my MyService appeared under Administrative Tools->Services

However, when I did a start of MyService, it started and then stopped :(

Service dialog:
The MyService on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service.

Anyone know why?

Upvotes: 1

Views: 4899

Answers (2)

Dzwiedziu-nkg
Dzwiedziu-nkg

Reputation: 723

First, if you use 64-bit JVM you must use 64-bit JavaService. Otherwise you will get following error in Windows Event logs:

The LoadLibrary function failed for the following reason: LoadLibrary is not a valid Win32 application.

Second, use -out and -err parameters of JavaService to log stdout and stderr from your java code.

Upvotes: 0

davidfrancis
davidfrancis

Reputation: 3849

Perhaps you need to specify the classpath?

You do this with a setting like this:

-Djava.class.path={JDK_HOME}\lib\tools.jar;<your jar 1>;<your jar 2>

As an example, we use a script to install JBoss, similar to the one discussed/shown here: https://community.jboss.org/wiki/JavaService

HTH

Upvotes: 1

Related Questions