如何从 Java 应用程序创建 Windows 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/68113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:53:33  来源:igfitidea点击:

How to create a windows service from java app

javawindows-services

提问by ideasculptor

I've just inherited a java application that needs to be installed as a service on XP and vista. It's been about 8 years since I've used windows in any form and I've never had to create a service, let alone from something like a java app (I've got a jar for the app and a single dependency jar - log4j). What is the magic necessary to make this run as a service? I've got the source, so code modifications, though preferably avoided, are possible.

我刚刚继承了一个需要在 XP 和 vista 上作为服务安装的 Java 应用程序。自从我以任何形式使用 windows 以来已经大约 8 年了,而且我从来没有创建过服务,更不用说从类似 java 应用程序的东西中创建的了(我有一个应用程序的 jar 和一个依赖项 jar - log4j )。使其作为服务运行所需的魔法是什么?我有源代码,所以代码修改,虽然最好避免,是可能的。

采纳答案by sblundy

I've had some luck with the Java Service Wrapper

在 Java Service Wrapper 上运气不错

回答by sblundy

I've used JavaServicebefore with good success. It hasn't been updated in a couple of years, but was pretty rock solid back when I used it.

我之前使用过JavaService并取得了成功。它已经有几年没有更新了,但是当我使用它时,它非常坚固。

回答by Hugh Buchanan

I didn't like the licensing for the Java Service Wrapper. I went with ActiveState Perl to write a service that does the work.

我不喜欢 Java Service Wrapper 的许可。我使用 ActiveState Perl 编写了一个可以完成这项工作的服务。

I thought about writing a service in C#, but my time constraints were too tight.

我想用 C# 编写一个服务,但我的时间太紧了。

回答by Kevin

I always just use sc.exe (see http://support.microsoft.com/kb/251192). It should be installed on XP from SP1, and if it's not in your flavor of Vista, you can download load it with the Vista resource kit.

我总是只使用 sc.exe(参见http://support.microsoft.com/kb/251192)。它应该从 SP1 开始安装在 XP 上,如果它不符合您的 Vista 风格,您可以使用 Vista 资源工具包下载并加载它。

I haven't done anything too complicated with Java, but using either a fully qualified command line argument (x:\java.exe ....) or creating a script with Ant to include depencies and set parameters works fine for me.

我没有对 Java 做过任何太复杂的事情,但是使用完全限定的命令行参数 (x:\java.exe ....) 或使用 Ant 创建脚本以包含依赖项并设置参数对我来说很好用。

回答by Ed Thomas

I think the Java Service Wrapperworks well. Note that there are three waysto integrate your application. It sounds like option 1 will work best for you given that you don't want to change the code. The configuration file can get a little crazy, but just remember that (for option 1) the program you're starting and for which you'll be specifying arguments, is their helper program, which will then start your program. They have an example configuration filefor this.

我认为Java Service Wrapper运行良好。请注意,可以通过三种方式集成您的应用程序。鉴于您不想更改代码,听起来选项 1 最适合您。配置文件可能会变得有点疯狂,但请记住(对于选项 1)您正在启动并为其指定参数的程序是它们的帮助程序,然后它将启动您的程序。为此,他们有一个示例配置文件

回答by Andrew Swan

Another good option is FireDaemon. It's used by some big shops like NASA, IBM, etc; see their web site for a full list.

另一个不错的选择是FireDaemon。它被一些大商店使用,比如 NASA、IBM 等;查看他们的网站以获取完整列表。

回答by Andrew Swan

I am currently requiring this to run an Eclipse-based application but I need to set some variables first that is local to that application. sc.exe will only allow executables but not scripts so I turned to autoexnt.exe which is part of the Windows 2003 resource kit. It restricts the service to a single batch file but I only need one batch script to be converted into a service.

我目前需要它来运行基于 Eclipse 的应用程序,但我需要先设置一些该应用程序本地的变量。sc.exe 将只允许可执行文件,但不允许脚本,所以我转向 autoexnt.exe,它是Windows 2003 资源工具包的一部分。它将服务限制为单个批处理文件,但我只需要将一个批处理脚本转换为服务。

ciao!

再见!

回答by Peter Smith

One more option is WinRun4J. This is a configurable java launcher that doubles as a windows service host (both 32 and 64 bit versions). It is open source and there are no restrictions on its use.

一种选择是WinRun4J。这是一个可配置的 Java 启动器,兼作 Windows 服务主机(32 位和 64 位版本)。它是开源的,对它的使用没有限制。

(full disclosure: I work on this project).

(完全披露:我从事这个项目)。

回答by NT_

JavaServiceis LGPL. It is very easy and stable. Highly recommended.

JavaService是 LGPL。它非常容易和稳定。强烈推荐。

回答by mcdon

Apache Commons Daemonis a good alternative. It has Procrunfor windows services, and Jsvcfor unix daemons. It uses less restrictive Apache license, and Apache Tomcat uses it as a part of itself to run on Windows and Linux! To get it work is a bit tricky, but there is an exhaustive articlewith working example.

Apache Commons Daemon是一个不错的选择。它Procrun为Windows服务,并JSVC用于UNIX守护进程。它使用限制较少的 Apache 许可,Apache Tomcat 将其作为自身的一部分在 Windows 和 Linux 上运行!让它工作有点棘手,但有一篇详尽的文章和工作示例。

Besides that, you may look at the bin\service.bat in Apache Tomcatto get an idea how to setup the service. In Tomcat they rename the Procrun binaries (prunsrv.exe -> tomcat6.exe, prunmgr.exe -> tomcat6w.exe).

除此之外,您可以查看Apache Tomcat中的 bin\service.bat 以了解如何设置服务。在 Tomcat 中,他们重命名 Procrun 二进制文件(prunsrv.exe -> tomcat6.exe,prunmgr.exe -> tomcat6w.exe)。

Something I struggled with using Procrun, your start and stop methods must accept the parameters (String[] argv). For example "start(String[] argv)" and "stop(String[] argv)" would work, but "start()" and "stop()" would cause errors. If you can't modify those calls, consider making a bootstrapper class that can massage those calls to fit your needs.

我在使用 Procrun 时遇到了一些困难,您的启动和停止方法必须接受参数 (String[] argv)。例如,“start(String[] argv)”和“stop(String[] argv)”会起作用,但“start()”和“stop()”会导致错误。如果您无法修改这些调用,请考虑创建一个引导程序类,该类可以根据您的需要调整这些调用。