无法使用init.d启动脚本执行jar文件

5

我按照这个教程进行操作:

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

我认为Spring Boot CLI 很有用。 - Spartan
你的应用程序中使用的是哪个Spring Boot版本? - Daniel Diehl
v1.2.6.RELEASE版本包含一个嵌入式Tomcat服务器。 - julien
1
@julien插件的v1.2.6版本不支持此功能。请尝试更新到1.3或更高版本。 - Stanislav
3个回答

6

您不能以这种方式运行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 Boot应用程序,请检查您的jar文件是否使用executable属性构建为true。

springBoot {
    executable = true
}

这样做可以让您按照所需的方式运行jar文件,而无需将其设置为可执行文件或需要任何其他库。
此外,根据评论,您正在使用的插件版本尚不支持此功能。您需要更新插件版本才能获取可执行的jar文件。根据插件源代码和提交历史记录,您至少需要1.3版本。

当我将这行代码添加到我的gradle.build文件中时,我遇到了这个问题:No such property: executable for class: org.springframework.boot.gradle.SpringBootPluginExtension_Decorated - julien
如果没有这个配置,您将无法使用您提供的用户指南中的解决方案。SpringBootPlugin 没有这样的属性意味着您可能使用的是不支持此功能的插件版本。 - Stanislav
注意,根据文档,此扩展自插件的1.3版本起可用。 - Stanislav

6
答案是不正确的,实际上你可以使用init.d将Spring Boot应用程序jar文件作为服务启动。甚至有一个Spring教程解释了如何做到这一点,正如Stanislav所指出的那样:https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html 问题可能在你的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。这些步骤是关于在Spring Boot服务中安装的。

这绝对是正确的,上面说这不可能的答案绝对是错误的。如果您正在使用Boot,则几乎可以肯定已经拥有Spring Boot Maven插件。Maven插件是一个惊喜。源和目标可以是1.8或更高版本。 - Ken Krueger
@KenKrueger 首先,这个问题是关于Gradle而不是Maven。无论如何运行一些本地的jar都是不可能的。要做到这一点,您需要构建一个可执行的jar,为此您需要使用Spring Boot插件并构建一个可执行的jar。就像在这个答案中一样,其中指出,您必须提供一个插件并将其可执行属性设置为true。 - Stanislav
答案与你的一样,要将jar作为服务运行,必须使用Spring Boot插件将其构建为可执行文件。但对于Gradle而言,因为问题是关于它的,而不是Maven。 - Stanislav

2

5
这是一个Spring Boot应用程序。如果正确配置了Spring Boot项目的构建,将会生成一个jar文件,可作为可执行文件使用,因为它会在jar文件前附加一个脚本。您可以在此处阅读更多信息:http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html - Stanislav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接