无法使用 init.d startscript 执行 jar

Can't execute jar with init.d startscript

我遵循了这个教程:

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

当我完全按照那里的描述执行此操作时,我在 Ubuntu 14.04 控制台中收到以下错误:

ubuntu@spring:/var/myapp$ /etc/init.d/myapp start
-bash: /etc/init.d/myapp: cannot execute binary file: Exec format error

您需要 "execute" 使用 java

的 jar

java -jar /var/myapp/myapp.jar

并且初始化脚本通常不会链接到可执行文件。

此 post 将向您展示如何为 java 应用程序创建初始化脚本。

Run a Java Application as a Service on Linux

您不能 运行 以这种方式生成 jar,因为它只是一个二进制文件。您必须 运行 它与已安装的 java (如 MrPsion 的回答中所述)

java -jar /var/myapp/myapp.jar

但是您不能为这样的命令创建符号链接。您可以使用上面的命令创建一个 bash 脚本,使其可执行并创建该脚本的符号链接。

或者,在 Ubuntu 中,您可以使用 binfmt-support。先安装就可以了

sudo apt-get install binfmt-support

然后使您的 jar 可执行

chmod a+x myapp.jar

然后你可以 运行 它(并用于符号链接)就像:

/var/myapp/myapp.jar

更新:

因为你有一个 Spring 引导应用程序,请检查你的 jar 是否是在 executable 属性 设置为 true

的情况下构建的
springBoot {
    executable = true
}

这应该让你 运行 你想要的方式你的 jar,whitout 使其成为可执行文件或需要任何额外的库。

另外,根据评论,您使用的插件版本暂不支持该功能。您必须更新插件版本才能获得可执行 jar。根据插件来源和 commit history 你至少需要 1.3 版本

答案不正确,您确实可以使用 init.d 启动 spring 引导应用程序 jar 作为服务。正如 Stanislav: https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

所指出的,甚至还有一个 spring 教程解释了如何操作

问题可能出在您的 maven 文件中。我遇到了同样的问题并解决了它,将以下内容添加到我的 maven 文件中:

<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>

详细说明在这里:https://springjavatricks.blogspot.com/2017/11/installing-spring-boot-services-in.html