我刚刚继承了一个java应用程序,需要在XP和vista上作为服务安装。我已经8年没有使用任何形式的windows了,我从来没有创建过服务,更不用说从java应用程序(我已经为应用程序准备了一个jar和一个依赖jar - log4j)。使其作为服务运行所必需的魔力是什么?我已经得到了源代码,所以代码修改(尽管最好避免)是可能的。
当前回答
使用Apache Commons Daemon,您现在可以拥有自定义的可执行名称和图标!您还可以获得一个自定义的Windows托盘显示器与您自己的名称和图标!
我现在有我的服务运行与我自己的名称和图标(prunsrv.exe),和系统托盘监视器(prunmgr.exe)也有我自己的自定义名称和图标!
Download the Apache Commons Daemon binaries (you will need prunsrv.exe and prunmgr.exe). Rename them to be MyServiceName.exe and MyServiceNamew.exe respectively. Download WinRun4J and use the RCEDIT.exe program that comes with it to modify the Apache executable to embed your own custom icon like this: > RCEDIT.exe /I MyServiceName.exe customIcon.ico > RCEDIT.exe /I MyServiceNamew.exe customTrayIcon.ico Now install your Windows service like this (see documentation for more details and options): > MyServiceName.exe //IS//MyServiceName \ --Install="C:\path-to\MyServiceName.exe" \ --Jvm=auto --Startup=auto --StartMode=jvm \ --Classpath="C:\path-to\MyJarWithClassWithMainMethod.jar" \ --StartClass=com.mydomain.MyClassWithMainMethod Now you have a Windows service of your Jar that will run with your own icon and name! You can also launch the monitor file and it will run in the system tray with your own icon and name. > MyServiceNamew.exe //MS//MyServiceName
其他回答
我目前需要运行一个基于eclipse的应用程序,但我需要先设置一些应用程序本地的变量。sc.exe只允许可执行文件,不允许脚本,所以我转向了Windows 2003资源包中的autoexnt.exe。它将服务限制为单个批处理文件,但我只需要将一个批处理脚本转换为服务。
嗨!
我在Java服务包装器方面有一些运气
JavaService是LGPL。这是非常容易和稳定的。强烈推荐。
通过结合使用外部内存和连接器API(从JDK16开始预览)与OpenJDK jextract项目来处理Windows服务的回调,然后使用jpackage来生成一个Windows EXE,然后可以注册为Windows服务,这是可能在100% Java代码中实现Windows服务。
请看这个例子,它概述了实现Windows服务所需的工作。所有Windows服务EXE必须为主入口点ServiceMain和服务控制处理程序提供回调,并在Advapi.DLL中使用API调用StartServiceCtrlDispatcherW, RegisterServiceCtrlHandlerExW和SetServiceStatus。
在具有外部内存结构的Java中,上述回调的流程是:
main()
Must register ServiceMain using StartServiceCtrlDispatcherW
Above call blocks until ServiceMain exits
void ServiceMain(int dwNumServicesArgs, MemoryAddress lpServiceArgVectors)
Must register SvcCtrlHandler using RegisterServiceCtrlHandlerExW
Use SetServiceStatus(SERVICE_START_PENDING)
Initialise app
Use SetServiceStatus(SERVICE_RUNNING)
wait for app shutdown notification
Use SetServiceStatus(SERVICE_STOPPED)
int SvcCtrlHandler(int dwControl, int dwEventType, MemoryAddress lpEventData, MemoryAddress lpContext)
Must respond to service control events and report back using SetServiceStatus
On receiving SERVICE_CONTROL_STOP reports SetServiceStatus(SERVICE_STOP_PENDING)
then set app shutdown notification
一旦完成Java应用程序,jpackage可以创建运行时+EXE,然后可以安装和注册为Windows服务。以管理员身份运行(=后面的空格很重要):
sc create YourJavaServiceName type= own binpath= "c:\Program Files\Your Release Dir\yourjavaservice.exe"
我不喜欢Java服务包装器的授权。我使用ActiveState Perl编写了一个完成这项工作的服务。
我想过用c#写一个服务,但是我的时间太紧了。
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列
- 如何运行一个类从Jar不是主类在其清单文件