我刚刚继承了一个java应用程序,需要在XP和vista上作为服务安装。我已经8年没有使用任何形式的windows了,我从来没有创建过服务,更不用说从java应用程序(我已经为应用程序准备了一个jar和一个依赖jar - log4j)。使其作为服务运行所必需的魔力是什么?我已经得到了源代码,所以代码修改(尽管最好避免)是可能的。
当前回答
这很简单,因为你必须加入快捷方式
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服务的详细说明
其他回答
我认为Java服务包装器工作得很好。请注意,集成应用程序有三种方法。如果您不想更改代码,那么听起来选项1最适合您。配置文件可能有点疯狂,但只要记住(对于选项1)您正在启动并将为其指定参数的程序是它们的辅助程序,然后它将启动您的程序。他们对此有一个示例配置文件。
另一个不错的选择是FireDaemon。它被一些大公司使用,比如NASA、IBM等;他们的网站上有完整的列表。
使用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服务包装器”,这似乎是Java服务包装器的一个很好的选择,因为它有更好的许可。它的目的还在于便于从JSW迁移到YAJSW。当然,对于我这个刚刚接触windows服务器并试图让Java应用程序作为服务运行的人来说,它非常容易使用。
我发现了一些其他的,但最终没有使用:
Java服务启动器我没有使用它,因为它看起来比YAJSW更复杂。我觉得这不是包装。 JSmooth创建窗口的服务不是它的主要目标,但可以做到。我没用这个,因为从2007年开始就没有活动了。
如果你使用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。
推荐文章
- 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不是主类在其清单文件