Home » Windows » How do I run a java .jar file as a windows service on windows server 2008?

How do I run a java .jar file as a windows service on windows server 2008?

Posted by: admin February 27, 2018 Leave a comment

Questions:

Hi everyone, how do I run a java .jar file as a Windows service on a windows 2008 server? I have a jar file called SomeJavaFile.jar located under the C:\SomeDirectory directory on a Windows Server 2008 box. I usually just run this jar manually in the command line with: java –cp SomeJavaFile.jar com.ctg.SomeJavaFile and I let it run 24/7.

The problem with our Windows server is if it restarts I need to run it as a service so it will automatically start as a service upon startup, because our processing for Vistakon runs 24/7. I have googled around and tried to use a service wrapper and the sc.exe command to create the service.

I put the wrapper service.exe in the C:\SomeDirectory\ . I use the following command to create it: sc.exe SomeJavaService binPath= “C:\SomeDirectory\service.exe \”java –jar C:\SomeDirectory\SomeJavaFile.jar\”” type= own start= auto error= ignore. This creates the SomeJavaService service correctly but when I try to start it I get an error that says the service on Local Computer started then stopped.

Some services stop automatically if they are not in use by other services or programs. Do I need to alter my sc.exe command to have the exact previous working command line maybe, by adding in the com.ctg.SomeJavaFile? So should I change This jar should just run in the background and constantly poll/ping the C:/poll directory then if there is data present it processes the data and sends an export file to another directory.

I have used this same .jar file for years successfully and it hasn’t changed, but I cannot get it to run as a windows service. This is the site I use to get the service wrapper http://code.google.com/p/simple-service-wrapper/. Any help would be greatly appreciated!

      Shane
Answers:

“winsw” is the standalone version of the Windows Service installer shipping with the Glassfish Java EE reference implementation.

Works well, but is not a fully polished product – I have used it for exactly this purpose for a couple of years. Requires .NET in some recent version on the Windows machine.

https://github.com/kohsuke/winsw

Questions:
Answers:

I think that the best bet would be wrap your java app with Procrun of Apache Commons Daemon .

Procrun is a set of applications that allow Windows users to wrap
(mostly) Java applications (e.g. Tomcat) as a Windows service.

The service can be set to automatically start when the machine boots
and will continue to run with no user logged onto the machine.

Questions:
Answers:

As per my analysis,

The Idle Solution will be writing a VC++ (.net) Windows Service creation program to launch the .bat (that triggers the jar file)/.exe as a System service with all the required call back methods to SCM.

Note : 1. Wrapping the process with sc.exe / srvany.exe would not work as it does not have any call back process to the SCM (Service Control Manager). 2. And java service Wrapper is a third party API (many are LGPL licensed).

Questions:
Answers:

If you start your Java code from commandline by using java -j some.jar does it run until you terminate the program, or does it stop by itself?

There needs to be a NON-Deamon Thread, that is running all the time. A JVM will terminate, if there is no thread running, that is not flagged as daemon.

If you have a little budget, buy an installer tool. I use instll4j. With that tool, you can create service launcher and install them during instllation.