Search code examples
javaservicesystemd

Unrecognized option when running Java via systemd


I am trying to run a Java application as a system service with systemd. The .service file for the service has the following command line:

ExecStart=/usr/bin/java -cp $CLASSPATH:/path/to/my.jar org.example.MainClass -myoption

where -myoption is an option intended to be passed to the main() method of the main class.

The command line works as expected when I launch it from bash; the option is passed to the main class.

However, when I try to start the service with systemctl start and then query its status with systemctl status, I see that the JVM terminated with:

Unrecognized option: -myoption

Apparently, the JVM is trying to make some sense of -myoption instead of passing it to the main class as an argument.

As the service is designed to exit upon the JVM being sent a SIGINT, I cannot use workarounds which will obscure the PID of the JVM from systemd.

How can I pass a command line option to the main class when launching it as a systemd service?


Solution

  • Environment variables in ExecStart must be enclosed in braces unless they are separate words (and cannot be used in the path to the executable itself).

    The following works as expected:

    ExecStart=/usr/bin/java -cp ${CLASSPATH}:/path/to/my.jar org.example.MainClass -myoption