5

I followed this Tutorial:

As init.d service

The executable jar has the usual start, stop, restart, and status commands. It will also set up a PID file in the usual /var/run directory and logging in the usual /var/log directory by default.

You just need to symlink your jar into /etc/init.d like so

Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service simply create a symlink:

$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp 

Then start the Service with:

/etc/init.d/myapp start 

When I do this exactly like it is described there, I get following error in the Ubuntu 14.04 console:

ubuntu@spring:/var/myapp$ /etc/init.d/myapp start -bash: /etc/init.d/myapp: cannot execute binary file: Exec format error 
4
  • I think that Spring Boot CLI is useful Commented May 25, 2016 at 13:52
  • Which spring-boot version are you using in your app? Commented May 26, 2016 at 3:33
  • The v1.2.6.RELEASE it starts with an embedded Tomcat Server.. Commented May 26, 2016 at 8:20
  • 1
    @julien v1.2.6 of the plugin doesn't support this feature. Try to update the version to 1.3 or newer Commented May 26, 2016 at 8:34

3 Answers 3

6

You can't run a jar this way, since it's just a binary file. You have to run it with the installed java (as it's mentioned in the MrPsion's answer)

java -jar /var/myapp/myapp.jar 

But you can't create a symlink to such a command. You can create a bash script, with the command above, make it executable and create a symlink to this script.

Alternatively, in Ubuntu you may use a binfmt-support. Just install it first

sudo apt-get install binfmt-support 

Then make your jar executable

chmod a+x myapp.jar 

And then you can run it (and use for the symlink) just as:

/var/myapp/myapp.jar 

Update:

Since you have a Spring Boot application, check whether your jar is build with the executable property set to true

springBoot { executable = true } 

This should let you run your jar the way you wanted, whitout make it an executable or require any additional libraries.

One more, according to the comments, the plugin version you're using doesn't support this feature yet. You have to update a plugin version in order to get an executable jar. According to the plugin sources and commit history you need atleast 1.3 version

Sign up to request clarification or add additional context in comments.

3 Comments

When I add this line to my gradle.build, I get this problem: No such property: executable for class: org.springframework.boot.gradle.SpringBootPluginExtension_Decorated
Without this configuration you can't use the solution from the user guide you've provided. The fact, SpringBootPlugin doesn't have such a property means, that you possibly have a version of the plugin, which doesn't sopport this feature.
Note, according to the docs, this extension available since 1.3 version of the plugin
6

The answers are incorrect, you can indeed launch a spring boot application jar as a service with init.d. There is even a spring tutorial explaining how to do it, as pointed out by Stanislav: https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

The problem was probably in your maven file. I had the same issue and solved it adding the following to my maven file:

<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.7.RELEASE</version> <configuration> <executable>true</executable> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> 

Detailed instructions here: https://springjavatricks.blogspot.com/2017/11/installing-spring-boot-services-in.html

3 Comments

This is definitely correct, and the answers above that say this is not possible are definitely incorrect. The Spring Boot Maven Plugin is something you almost certainly have already if you are using Boot. The Maven plugin is a surprise. Source and target can be 1.8 or greater.
@KenKrueger first of all, the question is about Gradle, not maven. And running some native jar is imposible anyway. To do it, you need to build an executable jar, for that you need to use the spring boot plugin and build an executable jar. Just the same as in this answer, where it is noted, that you have to provide a plugin and set it's executable property to true
The answer is the same as yours and saying that to run a jar as a service, you have to build it as executable with a spring boot plugin. But for Gradle, because the question if about it, not for Maven.
2

You need to "execute" the jar using java

java -jar /var/myapp/myapp.jar

and init scripts are not generally links to executable.

This post will show you how to create an init script for java applications.

Run a Java Application as a Service on Linux

1 Comment

It's a spring-boot application. Properly configured build of the spring-boot project will produce a jar, which will be handled as an executable, because it will embed an additional script in front of the jar. You can read it here docs.spring.io/spring-boot/docs/current/reference/html/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.