将Java jar作为Windows服务运行

时间:2017-12-08 10:02:19

标签: java windows-services java-service-wrapper

我有一个可执行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(Community Edition),服务启动了一段时间,但每次都停止了。包装器日志告诉我们:

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上有任何内容,请随时将其置于评论中。

1 个答案:

答案 0 :(得分:0)

我之前在高效的环境中使用过这种方法,所以我可以向您保证使用它是安全的。

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

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

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

相关问题