<p>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.</p>

[{'answer_id': 68140, 'author': 'sblundy', 'author_id': 4893, 'author_profile': 'https://Stackoverflow.com/users/4893', 'pm_score': 7, 'selected': True, 'text': '<p>I\'ve had some luck with <a href="http://wrapper.tanukisoftware.org/doc/english/introduction.html" rel="noreferrer">the Java Service Wrapper</a></p>\n'}, {'answer_id': 68847, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve used <a href="http://forge.objectweb.org/projects/javaservice/" rel="nofollow noreferrer">JavaService</a> before with good success. It hasn\'t been updated in a couple of years, but was pretty rock solid back when I used it.</p>\n'}, {'answer_id': 68884, 'author': 'Hugh Buchanan', 'author_id': 9307, 'author_profile': 'https://Stackoverflow.com/users/9307', 'pm_score': 2, 'selected': False, 'text': "<p>I didn't like the licensing for the Java Service Wrapper.  I went with ActiveState Perl to write a service that does the work.</p>\n\n<p>I thought about writing a service in C#, but my time constraints were too tight.</p>\n"}, {'answer_id': 68927, 'author': 'Kevin', 'author_id': 374643, 'author_profile': 'https://Stackoverflow.com/users/374643', 'pm_score': 2, 'selected': False, 'text': '<p>I always just use sc.exe (see <a href="http://support.microsoft.com/kb/251192" rel="nofollow noreferrer">http://support.microsoft.com/kb/251192</a>).  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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 68930, 'author': 'Ed Thomas', 'author_id': 8256, 'author_profile': 'https://Stackoverflow.com/users/8256', 'pm_score': 3, 'selected': False, 'text': '<p>I think the <a href="http://wrapper.tanukisoftware.org/doc/english/download.jsp" rel="nofollow noreferrer">Java Service Wrapper</a> works well. Note that there are <a href="http://wrapper.tanukisoftware.org/doc/english/integrate.html" rel="nofollow noreferrer">three ways</a> 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 <a href="http://wrapper.tanukisoftware.org/doc/english/props-example-config.html" rel="nofollow noreferrer">example configuration file</a> for this.</p>\n'}, {'answer_id': 69865, 'author': 'Andrew Swan', 'author_id': 10433, 'author_profile': 'https://Stackoverflow.com/users/10433', 'pm_score': 1, 'selected': False, 'text': '<p>Another good option is <a href="http://www.firedaemon.com/" rel="nofollow noreferrer">FireDaemon</a>. It\'s used by some big shops like NASA, IBM, etc; see their web site for a full list.</p>\n'}, {'answer_id': 118405, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>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 <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&amp;displaylang=en" rel="nofollow noreferrer">Windows 2003 resource kit</a>. It restricts the service to a single batch file but I only need one batch script to be converted into a service.</p>\n\n<p>ciao!</p>\n'}, {'answer_id': 847126, 'author': 'Peter Smith', 'author_id': 103715, 'author_profile': 'https://Stackoverflow.com/users/103715', 'pm_score': 5, 'selected': False, 'text': '<p>One more option is <a href="http://winrun4j.sourceforge.net/" rel="noreferrer" title="WinRun4J">WinRun4J</a>. 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. </p>\n\n<p>(full disclosure: I work on this project).</p>\n'}, {'answer_id': 1553442, 'author': 'NT_', 'author_id': 182094, 'author_profile': 'https://Stackoverflow.com/users/182094', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://forge.objectweb.org/projects/javaservice/" rel="nofollow noreferrer">JavaService</a> is LGPL. It is very easy and stable. Highly recommended.</p>\n'}, {'answer_id': 2518162, 'author': 'mcdon', 'author_id': 135280, 'author_profile': 'https://Stackoverflow.com/users/135280', 'pm_score': 7, 'selected': False, 'text': '<p><a href="http://commons.apache.org/daemon/index.html" rel="noreferrer">Apache Commons Daemon</a> is a good alternative. It has <a href="http://commons.apache.org/daemon/procrun.html" rel="noreferrer">Procrun</a> for windows services, and <a href="http://commons.apache.org/daemon/jsvc.html" rel="noreferrer">Jsvc</a> for unix daemons. It uses less restrictive Apache license, and Apache Tomcat uses it as a part of itself to run on Windows and Linux! To get it work is a bit tricky, but there is an <a href="http://web.archive.org/web/20090228071059/http://blog.platinumsolutions.com/node/234" rel="noreferrer">exhaustive article</a> with working example.</p>\n\n<p>Besides that, you may look at the bin\\service.bat in <a href="http://tomcat.apache.org/" rel="noreferrer">Apache Tomcat</a> to get an idea how to setup the service.  In Tomcat they rename the Procrun binaries (prunsrv.exe -> tomcat6.exe, prunmgr.exe -> tomcat6w.exe).</p>\n\n<p>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.</p>\n'}, {'answer_id': 3626452, 'author': 'atomicules', 'author_id': 208793, 'author_profile': 'https://Stackoverflow.com/users/208793', 'pm_score': 4, 'selected': False, 'text': '<p>Yet another answer is <a href="http://yajsw.sourceforge.net" rel="noreferrer">Yet Another Java Service Wrapper</a>, 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.</p>\n\n<p>Some others I found, but didn\'t end up using:</p>\n\n<ul>\n<li><a href="http://jslwin.sourceforge.net/-" rel="noreferrer">Java Service Launcher</a> I didn\'t use this because it looked more complicated to get working than YAJSW. I don\'t think this is a wrapper.</li>\n<li><a href="http://jsmooth.sourceforge.net/index.php" rel="noreferrer">JSmooth</a> Creating Window\'s services isn\'t its primary goal, but <a href="http://jsmooth.sourceforge.net/docs/jsmooth-doc.html#N103CD" rel="noreferrer">can be done</a>. I didn\'t use this because there\'s been no activity since 2007.</li>\n</ul>\n'}, {'answer_id': 4559593, 'author': 'pushNpop', 'author_id': 291090, 'author_profile': 'https://Stackoverflow.com/users/291090', 'pm_score': 2, 'selected': False, 'text': '<p>A pretty good comparison of different solutions is available at :\n<a href="http://yajsw.sourceforge.net/#mozTocId284533" rel="nofollow">http://yajsw.sourceforge.net/#mozTocId284533</a></p>\n\n<p>Personally like launch4j</p>\n'}, {'answer_id': 5954356, 'author': 'Thorbjørn Ravn Andersen', 'author_id': 53897, 'author_profile': 'https://Stackoverflow.com/users/53897', 'pm_score': 3, 'selected': False, 'text': '<p>Use "<a href="https://github.com/kohsuke/winsw" rel="nofollow noreferrer">winsw</a>" which was written for Glassfish v3 but works well with Java programs in general.</p>\n\n<p>Require .NET runtime installed.</p>\n'}, {'answer_id': 10756495, 'author': '11101101b', 'author_id': 875305, 'author_profile': 'https://Stackoverflow.com/users/875305', 'pm_score': 6, 'selected': False, 'text': '<p><strong>With <a href="http://commons.apache.org/daemon/index.html" rel="noreferrer">Apache Commons Daemon</a> you can now have a custom executable name and icon!</strong>  You can also get a custom Windows tray monitor with your own name and icon!</p>\n<p>I now have my service running with my own name and icon (prunsrv.exe), and the system tray monitor (prunmgr.exe) also has my own custom name and icon!</p>\n<ol>\n<li><p>Download the <a href="http://www.apache.org/dist/commons/daemon/binaries/windows/" rel="noreferrer">Apache Commons Daemon binaries</a> (you will need prunsrv.exe and prunmgr.exe).</p>\n</li>\n<li><p>Rename them to be <code>MyServiceName.exe</code> and <code>MyServiceNamew.exe</code> respectively.</p>\n</li>\n<li><p>Download <a href="http://winrun4j.sourceforge.net/" rel="noreferrer">WinRun4J</a> and use the <code>RCEDIT.exe</code> program that comes with it to modify the Apache executable to embed your own custom icon like this:</p>\n<pre><code>&gt; RCEDIT.exe /I MyServiceName.exe customIcon.ico\n&gt; RCEDIT.exe /I MyServiceNamew.exe customTrayIcon.ico\n</code></pre>\n</li>\n<li><p>Now install your Windows service like this (see <a href="http://commons.apache.org/daemon/procrun.html" rel="noreferrer">documentation</a> for more details and options):</p>\n<pre><code>&gt; MyServiceName.exe //IS//MyServiceName \\\n  --Install=&quot;C:\\path-to\\MyServiceName.exe&quot; \\\n  --Jvm=auto --Startup=auto --StartMode=jvm \\\n  --Classpath=&quot;C:\\path-to\\MyJarWithClassWithMainMethod.jar&quot; \\\n  --StartClass=com.mydomain.MyClassWithMainMethod\n</code></pre>\n</li>\n<li><p>Now you have a Windows service of your Jar that will run with your own icon and name!  You can also launch the monitor file and it will run in the system tray with your own icon and name.</p>\n<pre><code>&gt; MyServiceNamew.exe //MS//MyServiceName\n</code></pre>\n</li>\n</ol>\n'}, {'answer_id': 14553774, 'author': 'Giordano Maestro', 'author_id': 1885587, 'author_profile': 'https://Stackoverflow.com/users/1885587', 'pm_score': 5, 'selected': False, 'text': '<p>A simple way is the <a href="http://nssm.cc/" rel="noreferrer">NSSM Wrapper</a> Wrapper (<a href="http://giordanomaestro.blogspot.it/2013/01/running-java-applications-as-windows.html" rel="noreferrer">see my blog entry</a>).</p>\n'}, {'answer_id': 42204087, 'author': 'Ravi Parekh', 'author_id': 410439, 'author_profile': 'https://Stackoverflow.com/users/410439', 'pm_score': 2, 'selected': False, 'text': '<p>it\'s simple as you have to put shortcut in </p>\n\n<p><strong>Windows 7</strong>\n<code>C:\\users\\All Users\\Start Menu\\Programs\\Startup</code>(Admin)  or <code>User home directory(%userProfile%)</code></p>\n\n<p><strong>Windows 10 :</strong>\nIn Run   <code>shell:startup</code></p>\n\n<p>in it\'s property -> shortcut -> target - > <code>java.exe -jar D:\\..\\runJar.jar</code></p>\n\n<p><strong>NOTE: This will run only after you login</strong></p>\n\n<hr>\n\n<p><strong>With Admin Right</strong></p>\n\n<p><code>sc create serviceName binpath= "java.exe -jar D:\\..\\runJar.jar"</code> Will create windows service</p>\n\n<p>if you get <strong>timeout</strong> use <code>cmd /c D:\\JAVA7~1\\jdk1.7.0_51\\bin\\java.exe -jar d:\\jenkins\\jenkins.war</code> but even with this you\'ll get timeout but in background java.exe will be started. Check in task manager</p>\n\n<p><strong>NOTE: This will run at windows logon start-up(before sign-in, Based on service \'<code>Startup Type</code>\')</strong></p>\n\n<p><a href="https://stackoverflow.com/questions/3663331/creating-a-service-with-sc-exe-how-to-pass-in-context-parameters">Detailed explanation of creating windows service</a></p>\n'}, {'answer_id': 45443208, 'author': 'Alexey Lisyutenko', 'author_id': 8400888, 'author_profile': 'https://Stackoverflow.com/users/8400888', 'pm_score': 3, 'selected': False, 'text': '<p>If you use Gradle Build Tool you can try my <a href="https://github.com/alexeylisyutenko/windows-service-plugin" rel="noreferrer">windows-service-plugin</a>, which facilitates using of <a href="http://commons.apache.org/proper/commons-daemon/procrun.html" rel="noreferrer">Apache Commons Daemon Procrun</a>.  </p>\n\n<p>To create a java windows service application with the plugin you need to go through several simple steps.</p>\n\n<ol>\n<li><p>Create a main service class with the appropriate method.</p>\n\n<pre><code>public class MyService {\n\n    public static void main(String[] args) {\n        String command = "start";\n        if (args.length &gt; 0) {\n            command = args[0];\n        }\n        if ("start".equals(command)) {\n            // process service start function\n        } else {\n            // process service stop function\n        }\n    }\n\n}\n</code></pre></li>\n<li><p>Include the plugin into your <code>build.gradle</code> file.</p>\n\n<pre><code>buildscript {\n  repositories {\n    maven {\n      url "https://plugins.gradle.org/m2/"\n    }\n  }\n  dependencies {\n    classpath "gradle.plugin.com.github.alexeylisyutenko:windows-service-plugin:1.1.0"\n  }\n}\n\napply plugin: "com.github.alexeylisyutenko.windows-service-plugin"\n</code></pre>\n\n<p>The same script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:</p>\n\n<pre><code>plugins {\n  id "com.github.alexeylisyutenko.windows-service-plugin" version "1.1.0"\n}\n</code></pre></li>\n<li><p>Configure the plugin.</p>\n\n<pre><code>windowsService {\n  architecture = \'amd64\'\n  displayName = \'TestService\'\n  description = \'Service generated with using gradle plugin\'   \n  startClass = \'MyService\'\n  startMethod = \'main\'\n  startParams = \'start\'\n  stopClass = \'MyService\'\n  stopMethod = \'main\'\n  stopParams = \'stop\'\n  startup = \'auto\'\n}\n</code></pre></li>\n<li><p>Run <strong>createWindowsService</strong> gradle task to create a windows service distribution.</p></li>\n</ol>\n\n<p>That\'s all you need to do to create a simple windows service. The plugin will automatically download Apache Commons Daemon Procrun binaries, extract this binaries to the service distribution directory and create batch files for installation/uninstallation of the service.</p>\n\n<p>In <code>${project.buildDir}/windows-service</code> directory you will find service executables, batch scripts for installation/uninstallation of the service and all runtime libraries. \nTo install the service run <code>&lt;project-name&gt;-install.bat</code> and if you want to uninstall the service run <code>&lt;project-name&gt;-uninstall.bat</code>.\nTo start and stop the service use <code>&lt;project-name&gt;w.exe</code> executable.</p>\n\n<p>Note that the method handling service start should create and start a separate thread to carry out the processing, and then return. The main method is called from different threads when you start and stop the service.</p>\n\n<p>For more information, please read about the plugin and Apache Commons Daemon Procrun.</p>\n'}, {'answer_id': 45851799, 'author': 'Steephen', 'author_id': 1144157, 'author_profile': 'https://Stackoverflow.com/users/1144157', 'pm_score': 2, 'selected': False, 'text': '<p>With Java 8 we can handle this scenario without any external tools. <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javapackager.html" rel="nofollow noreferrer">javapackager</a> tool coming with java 8 provides an option to create self contained application bundles:</p>\n\n<p>-native type\nGenerate self-contained application bundles (if possible). Use the -B option to provide arguments to the bundlers being used. If type is specified, then only a bundle of this type is created. If no type is specified, all is used.</p>\n\n<p>The following values are valid for type:</p>\n\n<pre><code>-native type\nGenerate self-contained application bundles (if possible). Use the -B option to provide arguments to the bundlers being used. If type is specified, then only a bundle of this type is created. If no type is specified, all is used.\n\nThe following values are valid for type:\n\nall: Runs all of the installers for the platform on which it is running, and creates a disk image for the application. This value is used if type is not specified.\ninstaller: Runs all of the installers for the platform on which it is running.\nimage: Creates a disk image for the application. On OS X, the image is the .app file. On Linux, the image is the directory that gets installed.\ndmg: Generates a DMG file for OS X.\npkg: Generates a .pkg package for OS X.\nmac.appStore: Generates a package for the Mac App Store.\nrpm: Generates an RPM package for Linux.\ndeb: Generates a Debian package for Linux.\n</code></pre>\n\n<p>In case of windows refer the following <a href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javapackager.html" rel="nofollow noreferrer">doc</a> we can create msi or exe as needed.</p>\n\n<pre><code>exe: Generates a Windows .exe package.\nmsi: Generates a Windows Installer package.\n</code></pre>\n'}, {'answer_id': 68878128, 'author': 'Ram', 'author_id': 2756820, 'author_profile': 'https://Stackoverflow.com/users/2756820', 'pm_score': 1, 'selected': False, 'text': '<p>I have been using <a href="https://www.jar2exe.com/" rel="nofollow noreferrer">jar2exe</a> for last few years to run our Java applications as service on Windows. It provides an option to create an exe file which can be installed as Windows service.</p>\n'}, {'answer_id': 72658268, 'author': 'DuncG', 'author_id': 4712734, 'author_profile': 'https://Stackoverflow.com/users/4712734', 'pm_score': 0, 'selected': False, 'text': '<p>It\'s possible to implement a Windows service in 100% Java code by combining the use of <a href="https://jdk.java.net/panama/" rel="nofollow noreferrer">Foreign Memory and Linker API</a> (previewing from JDK16 upwards) with <a href="https://github.com/openjdk/jextract" rel="nofollow noreferrer">OpenJDK jextract project</a> to handle the Windows Service callbacks, and then use <a href="https://docs.oracle.com/en/java/javase/18/jpackage/packaging-overview.html" rel="nofollow noreferrer">jpackage</a> to produce a Windows EXE which can then be registered as a Windows Service.</p>\n<p>See this example which outlines the work needed to implement a <a href="https://learn.microsoft.com/en-gb/windows/win32/services/svc-cpp" rel="nofollow noreferrer">Windows service</a>. All Windows service EXE must provide callbacks for the main entrypoint <a href="https://learn.microsoft.com/en-gb/windows/win32/api/winsvc/nc-winsvc-lpservice_main_functionw" rel="nofollow noreferrer">ServiceMain</a> and <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nc-winsvc-lphandler_function" rel="nofollow noreferrer">Service Control Handler</a>, and use API calls <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatcherw" rel="nofollow noreferrer">StartServiceCtrlDispatcherW</a>, <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-registerservicectrlhandlerw" rel="nofollow noreferrer">RegisterServiceCtrlHandlerExW</a> and <a href="https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-setservicestatus" rel="nofollow noreferrer">SetServiceStatus</a> in <code>Advapi.DLL</code>.</p>\n<p>The flow of above callbacks in Java with Foreign Memory structures are:</p>\n<pre><code>main()\n    Must register ServiceMain using StartServiceCtrlDispatcherW\n    Above call blocks until ServiceMain exits\n        \nvoid ServiceMain(int dwNumServicesArgs, MemoryAddress lpServiceArgVectors)\n    Must register SvcCtrlHandler using RegisterServiceCtrlHandlerExW\n    Use SetServiceStatus(SERVICE_START_PENDING)\n    Initialise app\n    Use SetServiceStatus(SERVICE_RUNNING)\n    wait for app shutdown notification\n    Use SetServiceStatus(SERVICE_STOPPED)\n\nint SvcCtrlHandler(int dwControl, int dwEventType, MemoryAddress lpEventData, MemoryAddress lpContext)\n    Must respond to service control events and report back using SetServiceStatus\n    On receiving SERVICE_CONTROL_STOP reports SetServiceStatus(SERVICE_STOP_PENDING)\n        then set app shutdown notification\n</code></pre>\n<p>Once finished the Java application, jpackage can create runtime+EXE which can then be installed and registered as a Windows Service. Run as Adminstrator (spaces after = are important):</p>\n<pre><code> sc create YourJavaServiceName type= own binpath= &quot;c:\\Program Files\\Your Release Dir\\yourjavaservice.exe&quot;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/68113', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10273/']