views:

13118

answers:

12

I've just inherited a java application that needs to be installed as a service on XP and vista. It's been about 8 years since I've used windows in any form and I've never had to create a service, let alone from something like a java app (I've got a jar for the app and a single dependency jar - log4j). What is the magic necessary to make this run as a service? I've got the source, so code modifications, though preferably avoided, are possible.

+9  A: 

I've had some luck with the Java Service Wrapper

sblundy
Java Service Wrapper looks very useful. But I had a look at the feature list. You will just need to be aware that community version is not licensed for use on the server.
bmatthews68
That could be a problem. It was a few years ago I used it. Guess they decided to charge.
sblundy
This wrapper is used by a lot of open source projects, include several on Jakarta Apache, such as ActiveMQ.
Todd
What I like about the wrapper is that it can be configured to restart the JVM if it locks up.
Andrew Swan
http://wrapper.tanukisoftware.org/doc/english/licenseCommunity.htmlClosed Source UseThe GPL does not restrict private software from being developed for internal use which depends on software under the GPL as long as that software is never distributed without making the full source of the entire application available to all users.While we encourage corporate and government users to make use of either a Server or Development License Agreement, the Community License Agreement is acceptable as long as the application is for internal use or will be always be distributed along with its full src.
Vladimir
A: 

I've used JavaService before with good success. It hasn't been updated in a couple of years, but was pretty rock solid back when I used it.

+2  A: 

I didn't like the licensing for the Java Service Wrapper. I went with ActiveState Perl to write a service that does the work.

I thought about writing a service in C#, but my time constraints were too tight.

Hugh Buchanan
A: 

I always just use sc.exe (see http://support.microsoft.com/kb/251192). It should be installed on XP from SP1, and if it's not in your flavor of Vista, you can download load it with the Vista resource kit.

I haven't done anything too complicated with Java, but using either a fully qualified command line argument (x:\java.exe ....) or creating a script with Ant to include depencies and set parameters works fine for me.

Kevin
That allows you to start something as a service, but in my understanding it would then be detached, i.e. you couldn't stop it via Services or restart automatically, etc. I could be completely wrong though - only just started looking into this.
i5m
+4  A: 

I think the Java Service Wrapper works well. Note that there are three ways to integrate your application. It sounds like option 1 will work best for you given that you don't want to change the code. The configuration file can get a little crazy, but just remember that (for option 1) the program you're starting and for which you'll be specifying arguments, is their helper program, which will then start your program. They have an example configuration file for this.

Ed Thomas
+2  A: 

Another good option is FireDaemon. It's used by some big shops like NASA, IBM, etc; see their web site for a full list.

Andrew Swan
A: 

I am currently requiring this to run an Eclipse-based application but I need to set some variables first that is local to that application. sc.exe will only allow executables but not scripts so I turned to autoexnt.exe which is part of the Windows 2003 resource kit. It restricts the service to a single batch file but I only need one batch script to be converted into a service.

ciao!

A: 

I have write about several options available for that, please have a read to

http://hofmanndavid.blogspot.com/2008/11/run-java-application-as-windows.html

David Hofmann
+1  A: 

One more option is WinRun4J. This is a configurable java launcher that doubles as a windows service host (both 32 and 64 bit versions). It is open source and there are no restrictions on its use.

(full disclosure: I work on this project).

Peter Smith
+3  A: 

JavaService is LGPL. It is very easy and stable. Highly recommended.

_NT
+3  A: 

Apache Commons Daemon is a good alternative. It has Procrun for windows services, and Jsvc for unix daemons. There was a learning curve to using Procrun to setup the service. I looked at the bin\service.bat in Apache Tomcat to get an idea how to setup the service. In Tomcat they rename the Procrun binaries (prunsrv.exe -> tomcat6.exe, prunmgr.exe -> tomcat6w.exe).

Something I struggled with using Procrun, your start and stop methods must accept the parameters (String[] argv). For example "start(String[] argv)" and "stop(String[] argv)" would work, but "start()" and "stop()" would cause errors. If you can't modify those calls, consider making a bootstrapper class that can massage those calls to fit your needs.

mcdon
A: 

Yet another answer is Yet Another Java Service Wrapper, this seems like a good alternative to Java Service Wrapper as has better licensing. It is also intended to be easy to move from JSW to YAJSW. Certainly for me, brand new to windows servers and trying to get a Java app running as a service, it was very easy to use.

Some others I found, but didn't end up using:

  • Java Service Launcher I didn't use this because it looked more complicated to get working than YAJSW. I don't think this is a wrapper.
  • JSmooth Creating Window's services isn't its primary goal, but can be done. I didn't use this because there's been no activity since 2007.
i5m