gpt4 book ai didi

java - 将 Java jar 作为 Windows 服务运行

转载 作者:行者123 更新时间:2023-12-05 07:38:44 25 4
gpt4 key购买 nike

我有一个可执行 jar,我正尝试使用 sc.exe 创建一个 Windows 服务。我使用以下代码创建服务:

sc create "TestService" binPath= "C:\Program Files\Java\jdk1.6.0_03\jre\bin\java.exe -jar C:\abc\MainClass.jar"

服务已创建,但当我尝试启动服务时出现以下错误:

Error 1053: The service did not respond to the start or control request in a timely fashion.

后来我尝试使用 Java Service Wrapper(社区版),该服务启动了一段时间,但每次都停止。包装器日志显示如下内容:

Advice:
The Wrapper consists of a native component as well as a set of classes
which run within the JVM that it launches. The Java component of the
Wrapper must be initialized promptly after the JVM is launched or the
Wrapper will timeout, as just happened. Most likely the main class
specified in the Wrapper configuration file is not correctly initializing
the Wrapper classes:
com.MainClass
While it is possible to do so manually, the Wrapper ships with helper
classes to make this initialization processes automatic.
Please review the integration section of the Wrapper's documentation
for the various methods which can be employed to launch an application
within the Wrapper

谁能告诉我如何在不使用外部软件的情况下将 jar 作为 Windows 服务运行,因为我不能在客户端的产品环境中使用任何第三方应用程序。

如果不是,我需要在 Java Service Wrapper 中执行哪些其他配置才能启动服务。

我试图在 stackoverflow 上找到一些与此相关的信息,但我没有得到任何东西。如果有人对 stackoverflow 有任何了解,请随时发表评论。

最佳答案

我以前在生产环境中使用过这种方法,所以我可以向您保证它可以安全使用。

Jar 文件被包装在一个 exe 中,然后它被添加到 Windows 服务调度程序(或者你想调用它)。如果你有一个 maven 项目,这也很容易实现。

https://dzone.com/articles/installing-a-java-application-as-a-windows-service

编辑:你说你不能使用外部软件。使用这种方法,所需的一切都打包在 exe 文件中。包括 JRE,我希望您的客户的政策允许这样做。

关于java - 将 Java jar 作为 Windows 服务运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47711905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com