Link to home
Start Free TrialLog in
Avatar of meverest
meverestFlag for Australia

asked on

java application as a service

Status: Waiting for answer.

Points: 350  Date: Friday, March 17 2000 - 12:21PM EST    
Hi Folks,

i have a java application that i currently initiate via a batch file that calls the appropriate class.

is there any way that i can run this java application as a service that can be automatically started on server restart and controlled via the services applet?

thanks and regards.
Avatar of diakov
diakov

Hi I have followed the link you posted in the Java forum.
Yes, you have to use SRVANY and INSTSRV excutables from the NT resurce kit.

The first one is a service loader, the second one makes the necessary entry in the registry.

A structured step-by-step solution you can find at:
http://lama.supermedia.pl/docs/NT/srvany.htm

The only remark is that you have to run the main .class file using javaw instead of java, since you do not want aDOS box to appear. Make sure the claspath is set appropriatelly.

Cheers,
  Nik
Avatar of meverest

ASKER

so you are suggesting that i just run the java class as the srvany 'application' and then enter the java class as argument/s?

i am sure that i tried this already....

i will have another shot to see how it goes.

thanks for your input.
Yes. I have used it before and it works quite well.

There are detials like the user that runs the service (admin), some adjustments in the registry, etc, but this is quite straightforward.

in general, instsrv will be passed srvany as a parameter.

then srvany registry entry (as far as I recall "Parameters/"Appication") should be adjusted to point to the "javaw myclass.class").
and this is basically it. You can run almost any executable as a service this way. I'm sure there are experts on this forum that will help you with specific stuff if you run into problems.

Cheers,
  Nik
ok,

i have had another shot at this without success.  here is the commandline as run by the batch file:

"d:\jdk1.1.8\Bin\..\bin\java.exe" "-Dinstall.root=d:\MerchantServer" -classpath "d:\MerchantServer\jars\MerchantServer.jar;d:\MerchantServer\jars\iaik_jce_full.jar;d:\MerchantServer\jars\jdk11x_update.jar;.;d:\jdk1.1.8\Bin\..\classes;d:\jdk1.1.8\Bin\..\lib\classes.zip;d:\jdk1.1.8\Bin\..\lib\classes.jar;d:\jdk1.1.8\Bin\..\lib\rt.jar;d:\jdk1.1.8\Bin\..\lib\i18n.jar" au.com.camtech.ec.wrappers.tcpdaemon.TcpDaemon 5000

how ugly is that!?

ok, so i have put "d:\jdk1.1.8\Bin\..\bin\java.exe"  as the 'application value, and put "-Dinstall.root=d:\MerchantServer" -classpath "d:\MerchantServer\jars\MerchantServer.jar;d:\MerchantServer\jars\iaik_jce_full.jar;d:\MerchantServer\jars\jdk11x_update.jar;.;d:\jdk1.1.8\Bin\..\classes;d:\jdk1.1.8\Bin\..\lib\classes.zip;d:\jdk1.1.8\Bin\..\lib\classes.jar;d:\jdk1.1.8\Bin\..\lib\rt.jar;d:\jdk1.1.8\Bin\..\lib\i18n.jar" au.com.camtech.ec.wrappers.tcpdaemon.TcpDaemon 5000
 as 'arguments' value.

does this look right?

regards.


just realised that 'arguments' field for srvany should be 'AppParameters'

this does not work either.
Please, pay attention to my comments. In the link I provided steps 6,7,8 tell you the names of the entries.
The cmd line string should be then something like:
"d:\jdk1.1.8\Bin\..\bin\javaw.exe -Dinstall.root=d:\MerchantServer  -classpath d:\MerchantServer\jars\MerchantServer.jar;d:\MerchantServer\jars\iaik_jce_full.jar;d:\MerchantServer\jars\jdk11x_update.jar;.;d:\jdk1.1.8\Bin\..\classes;d:\jdk1.1.8\Bin\..\lib\classes.zip;d:\jdk1.1.8\Bin\..\lib\classes.jar;d:\jdk1.1.8\Bin\..\lib\rt.jar;d:\jdk1.1.8\Bin\..\lib\i18n.jar au.com.camtech.ec.wrappers.tcpdaemon.TcpDaemon 5000"

Note that I use javaw.exe, not java.exe

Also, the classpath can be put in the environment before hand.

Keep me posted if you have problems.

Cheers,
  Nik
thanks again diakov, but if i do it all in the 'Application' entry as suggested, i get an error 2140: An internal Windows NT error occured on starting the service.

and i have tried many combinations with java or javaw - these don't seem to make a difference (as in neither works)

by the way, this application is supposed to run a tcp server on port 5000 and this is how i can tell that it is not running (ie 'telnet' to port 5000 fails)

regards.
I have tried a simple application in java, it worked from the first try.

I did the following:

started the instsrv with:
instsrv test_java C:\freelance\test_java_as_service\srvany.exe

to create a service test_java

then with regedit I created a key Parameters in the test_jav service entry.
In this key I created 3 string values:
Application = "c:\jdk_path\bin\javaw.exe"
AppParameters = "-cp %classpath%;./ ViewData"
AppDirectory = "C:\freelance\test_java_as_service"

The from the control pannel I started the services applet, checked the "Allow service to interact with desktop" chekbox, because my application opens a GUI window and started the service manually.

It worked.

Now in your case, you have to choose appropriate startup directory in AppDirectory, enter the path + name of the javaw executable in the Application and paste all parameters in the AppParameters value.

Cheers,
  Nik
i am rejecting this answer for two reasons:

1.  i still can't get this to work with my particular java app,
2.  i want this question to remain visible in the top 10 questions.

thanks so much for your help so far diakov, but there must be something unusual about this particular class that does not allow it to run this way.

simple diagnosis suggests that finding required classes is failing.

i am particularly suspicious of the "-Dinstall.root=d:\MerchantServer" term, as when i enter just java.exe by itself, i do noit see any reference to '-Dinstall.root'

unfortunately i am not familiar enough with java calling requirements to figure that out...

regards.
I understand, try this:

-D set a system property "install.root" during loading of the application.
To see what the problem is, enable the "interacting with desktop" for this service (from control panel, sevices, start up), and use java.exe (not javaw.exe) to run in a DOS box. There you will be able to see the Java error messages, that lead to the service error. Then report back here.

Cheers,
  Nik
when i do this, the dos shell opens, then closes again with no text displayed.

:(

actually, as it turns out, the dos shell opens with a lot of output, then immediately closes within a split second.  i have tried to capture this output with prtsc, but i have not been able to do better than just before the text is displayed, or just after the box closes :(

some ideas of how to capture that output might prove useful.
Hm, that is strange. When I try my example service I start/stop it as a manual service - the dos box stays open. Do you use start/stop?

I'll thinkabout how this can be done.

An idea would be to write all this into a bat file and use not java.exe but cmd.exe as the srvany Application with AppParameters the .bat file. May be the dos box will stay open.

Try it out and tell me.

Meanwhile, JObject API might work for you :-)

Cheers,
  Nik
thanks,  i will.

> Meanwhile, JObject API might work for you :-)

it might, if i could figure out how to use it! ;-)

cheers.

G'day.

Using the JObject API is intrusive -- you have to alter the implementation of your application so that it registers itself as a listener for service control events and provide methods that allow some class in your application (which implements the ServiceControlListener interface) to catch the four NT service control events (stop, pause, continue, shutdown). If everything I just said sounds like so much gibberish, then forget it. Also, if you can't or don't want to change the application in any way, then forget it. Otherwise, you would have to have a development environment installed -- although I wouldn't particularly recommend MS VJ++. It appears from your classpath settings in previous posts that you already have jdk1.1.8 installed so you should be able to run C:\jdk1.1.8\bin\javac to compile.

Under which account are you trying to run this service? Is it the same one you normally use to logon to the system and run the application from a batch file? If "System Account" is checked in the "Log On As" option of the Services Startup... dialog, the environment running as a service may be (substantially) different than the environment when you run the batch file. PATH and CLASSPATH are just two of the environment variables which could be different. I notice that "." is one of the path components in your classpath. Is there any reliance on the current working directory? If so, this could be part of the problem.

Jim
ASKER CERTIFIED SOLUTION
Avatar of AlexJ030200
AlexJ030200

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This one solved my problem perfectly!

 Resource URL :        http://www.kcmultimedia.com/smaster
 
many thanks.
I'm glad it's done. Last year when I had the same problem that one was not listed and I used the third one which is only a document.

See you.
yes!

and it is most excellent - quick easy and no drama ;-)

i recommend it highly - thanks again.

cheers.