1

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?

Community
  • 1
  • 1
Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114

3 Answers3

4

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
}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • The bootRepackage command did the trick, thanks! Indeed the executable=true part is needed as well; I already had that but forgot to mention it (now updated). – Vlad Schnakovszki Apr 27 '16 at 13:24
0

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

I believe you need to use a full path for your source (i.e, /your/full/path/to/myjar.jar)

ikumen
  • 11,275
  • 4
  • 41
  • 41
0

One of the ways to execute your application through command line.

  1. When you have apply plugin:'java' then the spring looks for bootJar and disables the Jar task by default.

  2. Inorder to build the fully executable jar then you can include this code

bootJar {

launchScript() }

And then clean your build and run bootJar task once. Later navigate to the gradle folder and run

./gradlew bootRun

This runs your spring-boot app. I think there are many other ways like java -jar <yourapplication.jar>. But I follow this way. Hope it helps.

Sandeep Amarnath
  • 5,463
  • 3
  • 33
  • 43