Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't execute jar with init.d startscript

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
like image 710
julien Avatar asked May 25 '16 12:05

julien


Video Answer


2 Answers

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

like image 109
Stanislav Avatar answered Sep 28 '22 03:09

Stanislav


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

like image 34
pama Avatar answered Sep 28 '22 04:09

pama