我刚刚继承了一个java应用程序,需要在XP和vista上作为服务安装。我已经8年没有使用任何形式的windows了,我从来没有创建过服务,更不用说从java应用程序(我已经为应用程序准备了一个jar和一个依赖jar - log4j)。使其作为服务运行所必需的魔力是什么?我已经得到了源代码,所以代码修改(尽管最好避免)是可能的。
当前回答
使用Java 8,我们可以在没有任何外部工具的情况下处理这种情况。Java 8附带的Javapackager工具提供了一个创建自包含应用程序包的选项:
原生类型 生成自包含的应用程序包(如果可能的话)。使用-B选项为正在使用的绑定器提供参数。如果指定了type,则只创建该类型的bundle。如果没有指定类型,则使用all。
以下值对type有效:
-native type
Generate self-contained application bundles (if possible). Use the -B option to provide arguments to the bundlers being used. If type is specified, then only a bundle of this type is created. If no type is specified, all is used.
The following values are valid for type:
all: Runs all of the installers for the platform on which it is running, and creates a disk image for the application. This value is used if type is not specified.
installer: Runs all of the installers for the platform on which it is running.
image: Creates a disk image for the application. On OS X, the image is the .app file. On Linux, the image is the directory that gets installed.
dmg: Generates a DMG file for OS X.
pkg: Generates a .pkg package for OS X.
mac.appStore: Generates a package for the Mac App Store.
rpm: Generates an RPM package for Linux.
deb: Generates a Debian package for Linux.
在windows的情况下,参考以下文档,我们可以根据需要创建msi或exe。
exe: Generates a Windows .exe package.
msi: Generates a Windows Installer package.
其他回答
我不喜欢Java服务包装器的授权。我使用ActiveState Perl编写了一个完成这项工作的服务。
我想过用c#写一个服务,但是我的时间太紧了。
使用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
在过去几年里,我一直在使用jar2exe在Windows上运行我们的Java应用程序作为服务。它提供了一个选项来创建一个exe文件,可以作为Windows服务安装。
另一种选择是WinRun4J。这是一个可配置的java启动器,可作为windows服务主机(32位和64位版本)。它是开源的,使用上没有限制。
(完全披露:我参与了这个项目)。
这很简单,因为你必须加入快捷方式
Windows 7 C:\users\所有用户\开始菜单\程序\启动(Admin)或用户主目录(%userProfile%)
Windows 10: 在“Run shell:startup”中
在它的属性->快捷方式->目标-> java.exe -jar D:\..\runJar.jar
注意:只有在您登录后才会运行
拥有管理员权限
sc create serviceName binpath= "java.exe -jar D:\..\runJar.jar"将创建windows服务
如果超时,使用cmd /c D:\JAVA7~1\jdk1.7.0_51\bin\java.exe -jar D:\ jenkins\jenkins。战争,但即使这样,你会得到超时,但在后台java.exe将启动。检入任务管理器
注意:这将在windows登录启动时运行(在登录之前,基于服务“启动类型”)
创建windows服务的详细说明
推荐文章
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?