Apache Commons Daemon will run your Java program as Linux daemon or WinNT Service.

Answer from Bahaa Zaid on Stack Overflow
๐ŸŒ
Tanuki Software
wrapper.tanukisoftware.com โ€บ doc โ€บ english โ€บ qna-unix-daemon.html
How to run a Java application as a UNIX Daemon - Q&A - Java Service Wrapper
It offers automatic detection of the service management tools (also known as init systems) available on your OS, or lets you configure a specific system to use when launching your application as a daemon.
Top answer
1 of 11
37

Apache Commons Daemon will run your Java program as Linux daemon or WinNT Service.

2 of 11
32

If you can't rely on Java Service Wrapper cited elsewhere (for instance, if you are running on Ubuntu, which has no packaged version of SW) you probably want to do it the old fashioned way: have your program write its PID in /var/run/$progname.pid, and write a standard SysV init script (use for instance the one for ntpd as an example, it's simple) around it. Preferably, make it LSB-compliant, too.

Essentially, the start function tests if the program is already running (by testing if /var/run/$progname.pid exists, and the contents of that file is the PID of a running process), and if not run

logfile=/var/log/$progname.log
pidfile=/var/run/$progname.pid
nohup java -Dpidfile=$pidfile $jopts $mainClass </dev/null > $logfile 2>&1

The stop function checks on /var/run/$progname.pid, tests if that file is the PID of a running process, verifies that it is a Java VM (so as not to kill a process that simply reused the PID from a dead instance of my Java daemon) and then kills that process.

When called, my main() method will start by writing its PID in the file defined in System.getProperty("pidfile").

One major hurdle, though: in Java, there is no simple and standard way to get the PID of the process the JVM runs in.

Here is what I have come up with:

private static String getPid() {
    File proc_self = new File("/proc/self");
    if(proc_self.exists()) try {
        return proc_self.getCanonicalFile().getName();
    }
    catch(Exception e) {
        /// Continue on fall-back
    }
    File bash = new File("/bin/bash");
    if(bash.exists()) {
        ProcessBuilder pb = new ProcessBuilder("/bin/bash","-c","echo $PPID");
        try {
            Process p = pb.start();
            BufferedReader rd = new BufferedReader(new InputStreamReader(p.getInputStream()));
            return rd.readLine();
        }
        catch(IOException e) {
            return String.valueOf(Thread.currentThread().getId());
        }
    }
    // This is a cop-out to return something when we don't have BASH
    return String.valueOf(Thread.currentThread().getId());
}
๐ŸŒ
Apache Commons
commons.apache.org โ€บ daemon
Daemon : Java based daemons or services โ€“ Apache Commons Daemon
May 31, 2026 - Apache Commons, Apache Commons Daemon, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
๐ŸŒ
Source-code
source-code.biz โ€บ snippets โ€บ java โ€บ 7.htm
How to run a Java Program as a daemon (service) on Linux (SUSE/openSUSE) using a shell script
Run the Java daemon as a different (non-root) Linux user. Log error messages and redirect StdOut/StdErr output into a log file. Install/uninstall the daemon as a Linux service.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ daemon-thread-java
Java Daemon Thread - GeeksforGeeks
April 24, 2026 - Garbage Collection: The Garbage Collector (GC) in Java runs as a daemon thread. Background Monitoring: Daemon threads can monitor the state of application components, resources, or connections. Logging and Auditing Services: Daemon threads can be used to log background activities continuously.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java concurrency โ€บ daemon threads in java
Daemon Threads in Java | Baeldung
1 month ago - So, we learned that Java offers two types of platform threads: user threads and daemon threads. While user threads are high-priority threads, daemon threads are low-priority threads whose only role is to provide services to user threads.
๐ŸŒ
Quora
quora.com โ€บ What-are-the-mechanics-of-turning-a-Java-program-into-a-daemon
What are the mechanics of turning a Java program into a daemon? - Quora
Answer (1 of 2): 1. Java Service Wrapper This is a piece of software designed to do exactly what you asked. It is cross platform and has many nice features. It offers several versions, some of which cost money. Here is their website: Download Java Service Wrapper. If you are on Linux systems, t...
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ daemon thread in java
Daemon Thread in Java - Scaler Topics
April 8, 2024 - Daemon thread in Java is a service provider thread that provides services to the user thread. Its life depends on the mercy of user threads i.e. when all the user threads die, JVM terminates this thread automatically.
๐ŸŒ
Wordpress
velmuruganv.wordpress.com โ€บ 2016 โ€บ 02 โ€บ 23 โ€บ how-to-daemonize-a-java-program
How to Daemonize a Java Program? | Velmurugan Velayutham
February 23, 2016 - Install daemon tools from the URL https://cr.yp.to/daemontools/install.html and follow the instruction mentioned there,for any issues please try instructions error.h ยท Create a file at /etc/init/svscan.conf and add the below lines.(only required for cent-os-6.7) start on runlevel [12345] stop on runlevel [^12345] respawn exec /command/svscanboot ยท 3. Create a new script named run inside /service/vm/ folder and add the below lines. #!/bin/bash echo starting VM exec java -jar /root/learning-/daemon-java/vm.jar
๐ŸŒ
University of Illinois
geom.uiuc.edu โ€บ ~daeron โ€บ docs โ€บ javaguide โ€บ java โ€บ threads โ€บ daemon.html
Daemon Threads
Any Java thread can be a daemon thread. Daemon threads are service providers for other threads or objects running in the same process as the daemon thread.
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ daemon thread in java | creation, uses & more (+examples)
Daemon Thread In Java | Creation, Uses & More (+Examples)
December 13, 2024 - A daemon thread in Java programming is a background thread that supports the execution of user threads by providing auxiliary services. Unlike regular threads, daemon threads are not intended to perform tasks critical to the application; instead, ...
Top answer
1 of 3
20

Services on Linux are just shell scripts which start background processes. Have a look in /etc/init.d - you can open the files in a text editor. All you need is a bash script which responds to the parameters start and stop in an appropriate way (eg. start will start your service and record the process ID in a known location, stop will kill the process with the PID from the file you created), and then place it in /etc/init.d.

Have a look at Init Scripts and An introduction to services, runlevels, and rc.d scripts

2 of 3
13

As far as I know there isn't a Maven plugin for either Apache Daemon or Akuma. Though you could attempt to invoke them from within a Maven build by using the maven-exec-plugin.


As far as your companies reservations about using GPL-licensed products, it's worth reading up on the implications of use. It is not as virulent as corporations fear. Here's an interpretation of the GPL. It of course doesn't carry any weight in law (and may not be correct or supported by precedent, I am not a lawyer), but might be sufficient to allow you to start a conversation with your legal people.

From the referenced page:

Simply combining a copyrighted work with another work does not create a derivative work. The original copyrighted work must be modified in some way. The resulting derivative work must itself "represent an original work of authorship." So if the licensee does not modify the original GPL-licensed program, but merely runs it, he is not creating a derivative work.


There is the Appassembler Maven plugin that I think does what you need (though it does create JSW wrappers). It creates a shell script (and a bat file), and collects all the application jars into a directory. It can optionally be configured to create JSW-based Daemon configurations.

Here is an example configuration that will generate the standalone application in the target/appassembler folder, and generate the JSW wrapper files in the target/appassembler/jsw/myApp directory. Note the assemble goal is bound to the integration-test phase to ensure the project's jar is created. To generate the output run mvn verify or to just generate the service wrappers run mvn package:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>assemble-standalone</id>
        <phase>integration-test</phase>
        <goals>
          <goal>assemble</goal>
        </goals>
        <configuration>
          <programs>
            <program>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <name>myShellScript</name>
            </program>
          </programs>
          <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
          </platforms>
          <!--collect all jars into the lib directory-->
          <repositoryLayout>flat</repositoryLayout>
          <repositoryName>lib</repositoryName>
        </configuration>
      </execution>
      <execution>
        <id>generate-jsw-scripts</id>
        <phase>package</phase>
        <goals>
          <goal>generate-daemons</goal>
        </goals>
        <configuration>
          <!--declare the JSW config -->
          <daemons>
            <daemon>
              <id>myApp</id>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <commandLineArguments>
                <commandLineArgument>start</commandLineArgument>
              </commandLineArguments>
              <platforms>
                <platform>jsw</platform>
              </platforms>              
            </daemon>
          </daemons>
          <target>${project.build.directory}/appassembler</target>
        </configuration>
      </execution>
    </executions>
  </plugin>

For reference the generated files are as follows:

myApp\bin\myApp
myApp\bin\myApp.bat
myApp\bin\wrapper-linux-x86-32
myApp\bin\wrapper-macosx-universal-32
myApp\bin\wrapper-solaris-x86-32
myApp\bin\wrapper-windows-x86-32.exe
myApp\conf\wrapper.conf
myApp\lib\libwrapper-linux-x86-32.so
myApp\lib\libwrapper-macosx-universal-32.jnilib
myApp\lib\libwrapper-solaris-x86-32.so
myApp\lib\wrapper-windows-x86-32.dll
myApp\lib\wrapper.jar
๐ŸŒ
Linuxtopia
linuxtopia.org โ€บ online_books โ€บ programming_books โ€บ thinking_in_java โ€บ TIJ315_005.htm
Thinking in Java 13: Concurrency - Daemon threads
A โ€œdaemonโ€ thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated. Conversely, if there are any non-daemon ...
๐ŸŒ
GitHub
github.com โ€บ wizardkyn โ€บ DbDump
GitHub - wizardkyn/DbDump: Sample Java Daemon as Windows Service using Apache commons daemon procrun
CountService.exe //IS//CountService --DisplayName="Count Service" --Startup=auto ^ --Description="Sample Winodws Service as Java Daemon" ^ --Install="H:\job\procrun\CountService.exe" ^ --Jvm="C:\Program Files\Java\jdk1.8.0_05\jre\bin\server\jvm.dll" --StartMode=jvm --StopMode=jvm ^ --Classpath="H:\job\procrun\dbDump.jar" ^ --StartClass=com.bcg.dbdump.CountService --StartMethod=start ^ --StopClass=com.bcg.dbdump.CountService --StopMethod=stop ^ --LogPath="H:\job\procrun\log" --LogLevel=debug"
Author ย  wizardkyn
๐ŸŒ
Apache Commons
commons.apache.org โ€บ daemon โ€บ jsvc.html
Daemon : Java Service โ€“ Apache Commons Daemon
Apache Commons, Apache Commons Daemon, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
๐ŸŒ
Mwells
mwells.org โ€บ coding โ€บ 2016 โ€บ deamonize-java-service-linux
Daemonize a Java service on Linux the simple way
January 22, 2016 - Next I tried Apache Commons Daemon (jsvc), which was easy to integrate and worked fine with Maven, but I found the commandline very finicky. It took a long time to get the commandline arguments correct. I was attracted to jsvc because it was used by Tomcat, but then I found a post that said they removed it from Tomcat deployed on Ubuntu and Debian. There is a much simplier way. First, I implemented a Java shutdown hook so when the process receives an interrupt, the service will exit cleanly.