javagradlejarspring-bootinit.d

Error Running SpringBoot Jar using init.d


I have a SpringBoot application, for which I use gradle :myapp:jar to generate an executable jar. In addition, I also had to use chmod 755 myapp.jar to make the jar executable.

This is the gradle code for generating the jar, as described here:

springBoot { executable = true }

springBoot {
    executable = true
}

jar {
    baseName = 'myapp'
    version = '0.1.0'
    manifest {
        attributes "Main-Class": "eu.myapp.Application"
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

I then create a symlink to init.d, as described here in order to use start/stop/status. The command I am executing to create the symlink is this:

sudo ln -fs myjar.jar /etc/init.d/myjar

After this, I can do:

sudo /etc/init.d/myapp start

The issue is that when I execute this command, I get the following error:

/etc/init.d/myapp: 1: /etc/init.d/myapp: PK: not found
/etc/init.d/myapp: 2: /etc/init.d/myapp�z�H: not found
/etc/init.d/myapp: 3: /etc/init.d/myapp: Syntax error: ")" unexpected

Looking it up online, it appears to be related to a shebang issue, which can be fixed by adding #!/bin/bash at the beginning of the file. However, since I'm running a .jar generated by gradle, where would I need to add this line? Alternatively, how can I fix the above error?


Solution

  • The jar task won't produce a fully executable jar suitable for launched via init.d. Instead, you need to run the bootRepackage task that's provided by Spring Boot's Gradle plugin and you also need to have configured it to produce a fully executable jar. To do so, add the following to your build.gradle:

    springBoot {
        executable = true
    }