I am trying to run embedded jetty as a windows service using Apache Commons Daemon 1.2.0. I am able to run the embedded jetty jar using java -jar and start the server successfully, however when I try to run the same using Apache Daemon it does not work.
I am using maven shade plugin to generate the embedded jar.
When starting the service after installing using prunsrv with the below command, following is printed in the logs and the services does not start:
prunsrv.exe //IS//winserv --Description="winserv jetty Service" --Classpath="D:\procrun\winserv.jar" --StartMode=jvm --StartClass=com.ebdjetty.winserv.App --StartParams=start --StdOutput=auto --StdError=auto --JavaHome="C:\Program Files (x86)\Java\jre1.8.0_211\bin" --StartMethod=startServer --Jvm="C:\Program Files (x86)\Java\jre1.8.0_211\bin\client\jvm.dll" --StopMode=jvm --StopMethod=stopServer --StopParams=stop --StopClass=com.ebdjetty.winserv.App --StdOutput=D:\procrun\logs\stdOut.log --StdError=D:\procrun\logs\stdErr.log --LogLevel=Debug --LogPath=D:\procrun\logs\ --LogPrefix=winserv-daemon
winserv-daemon log:
[2019-08-19 19:22:06] [debug] ( prunsrv.c:1754) [16324] Apache Commons Daemon procrun log initialized.
[2019-08-19 19:22:06] [info] ( prunsrv.c:1758) [16324] Apache Commons Daemon procrun (1.2.0.0 32-bit) started.
[2019-08-19 19:22:06] [info] ( prunsrv.c:1668) [16324] Running Service 'winserv'...
[2019-08-19 19:22:06] [debug] ( prunsrv.c:1441) [ 2984] Inside ServiceMain...
[2019-08-19 19:22:06] [debug] ( prunsrv.c:904 ) [ 2984] reportServiceStatusE: dwCurrentState = 2, dwWin32ExitCode = 0, dwWaitHint = 3000, dwServiceSpecificExitCode = 0.
[2019-08-19 19:22:06] [info] ( prunsrv.c:1196) [ 2984] Starting service...
When stating using windows Admin Tools > Services, I get:
Error 1067:The process terminated unexpectedly.
Tried this Service will not start: error 1067: the process terminated unexpectedly but it did not help.
Below is the code to create the embedded jetty jar: Class to start jetty:
package com.ebdjetty.winserv;
import org.eclipse.jetty.server.Server;
public class App
{
static Server server = null;
public static void main( String[] args )
{
startServer(args);
}
public static void startServer(String[] args) {
System.out.println("In startServer method.");
try {
start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void stopServer(String[] args) {
System.out.println("In stopServer method.");
try {
stop();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void start()throws Exception{
server = new Server(9092);
System.out.println("Starting server on port 9092");
server.start();
System.out.println("Started");
server.join();
}
public static void stop()throws Exception{
System.out.println("Stopping Server");
server.stop();
}
}
pom.xml used to generate the embedded jetty jar using maven shade:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ebdjetty.winserv</groupId>
<artifactId>AppEbdTest</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>winserv</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<servlet-api.version>3.1.0</servlet-api.version>
<jetty.version>9.4.15.v20190215</jetty.version>
<!-- Plugin versions -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.4.15.v20190215</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
</dependencies>
<build>
<finalName>winserv</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.ebdjetty.winserv.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>