我刚刚继承了一个java应用程序,需要在XP和vista上作为服务安装。我已经8年没有使用任何形式的windows了,我从来没有创建过服务,更不用说从java应用程序(我已经为应用程序准备了一个jar和一个依赖jar - log4j)。使其作为服务运行所必需的魔力是什么?我已经得到了源代码,所以代码修改(尽管最好避免)是可能的。
当前回答
一种简单的方法是使用NSSM Wrapper Wrapper(请参阅我的博客文章)。
其他回答
如果你使用Gradle构建工具,你可以试试我的windows-service-plugin,它可以方便地使用Apache Commons Daemon Procrun。
要用这个插件创建一个java windows服务应用程序,你需要经过几个简单的步骤。
Create a main service class with the appropriate method. public class MyService { public static void main(String[] args) { String command = "start"; if (args.length > 0) { command = args[0]; } if ("start".equals(command)) { // process service start function } else { // process service stop function } } } Include the plugin into your build.gradle file. buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "gradle.plugin.com.github.alexeylisyutenko:windows-service-plugin:1.1.0" } } apply plugin: "com.github.alexeylisyutenko.windows-service-plugin" The same script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1: plugins { id "com.github.alexeylisyutenko.windows-service-plugin" version "1.1.0" } Configure the plugin. windowsService { architecture = 'amd64' displayName = 'TestService' description = 'Service generated with using gradle plugin' startClass = 'MyService' startMethod = 'main' startParams = 'start' stopClass = 'MyService' stopMethod = 'main' stopParams = 'stop' startup = 'auto' } Run createWindowsService gradle task to create a windows service distribution.
这就是创建一个简单的windows服务所需要做的全部工作。该插件将自动下载Apache Commons Daemon Procrun二进制文件,将这些二进制文件解压缩到服务分发目录,并创建批处理文件用于安装/卸载服务。
在${项目。在buildDir}/windows-service目录下,你会找到服务的可执行文件,用于安装/卸载服务的批处理脚本和所有运行时库。 安装服务请执行<project-name>-install.bat,卸载服务请执行<project-name>-uninstall.bat。 要启动和停止服务,请使用<project-name>w.exe可执行文件。
注意,方法处理服务启动应该创建并启动一个单独的线程来执行处理,然后返回。当您启动和停止服务时,从不同的线程调用main方法。
更多信息,请阅读插件和Apache Commons Daemon Procrun。
JavaService是LGPL。这是非常容易和稳定的。强烈推荐。
另一种选择是WinRun4J。这是一个可配置的java启动器,可作为windows服务主机(32位和64位版本)。它是开源的,使用上没有限制。
(完全披露:我参与了这个项目)。
我目前需要运行一个基于eclipse的应用程序,但我需要先设置一些应用程序本地的变量。sc.exe只允许可执行文件,不允许脚本,所以我转向了Windows 2003资源包中的autoexnt.exe。它将服务限制为单个批处理文件,但我只需要将一个批处理脚本转换为服务。
嗨!
另一个不错的选择是FireDaemon。它被一些大公司使用,比如NASA、IBM等;他们的网站上有完整的列表。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap