Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从java应用程序创建windows服务_Java_Windows Services - Fatal编程技术网

如何从java应用程序创建windows服务

如何从java应用程序创建windows服务,java,windows-services,Java,Windows Services,我刚刚继承了一个java应用程序,它需要作为服务安装在XP和vista上。我已经8年没有以任何形式使用windows了,我从来没有创建过服务,更不用说从java应用程序之类的东西(我有一个应用程序的jar和一个依赖项jar-log4j)。让它作为服务运行需要什么魔力?我已经获得了源代码,因此代码修改是可能的,尽管最好避免修改。我很幸运地使用了我以前使用过的代码,并取得了很好的成功。它已经有几年没有更新了,但在我使用它的时候,它已经非常可靠了。我不喜欢Java服务包装器的许可。我使用ActiveS

我刚刚继承了一个java应用程序,它需要作为服务安装在XP和vista上。我已经8年没有以任何形式使用windows了,我从来没有创建过服务,更不用说从java应用程序之类的东西(我有一个应用程序的jar和一个依赖项jar-log4j)。让它作为服务运行需要什么魔力?我已经获得了源代码,因此代码修改是可能的,尽管最好避免修改。

我很幸运地使用了我以前使用过的代码,并取得了很好的成功。它已经有几年没有更新了,但在我使用它的时候,它已经非常可靠了。

我不喜欢Java服务包装器的许可。我使用ActiveState Perl编写了一个服务来完成这项工作

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

我总是使用sc.exe(请参阅)。它应该从SP1安装在XP上,如果它不是您喜欢的Vista,您可以下载并使用Vista资源工具包加载它


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

我认为效果很好。请注意,集成您的应用程序有很多方法。考虑到您不想更改代码,选项1似乎最适合您。配置文件可能会有点疯狂,但请记住(对于选项1),您正在启动并将为其指定参数的程序是它们的帮助程序,然后它将启动您的程序。他们对此有一个很好的选择。

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

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

再见

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

(完全披露:我在这个项目上工作)。

是LGPL。这是非常容易和稳定的。强烈推荐。

是一个很好的选择。它对windows服务和unix守护进程都有好处。它使用限制较少的Apache许可证,Apache Tomcat将其作为自身的一部分在Windows和Linux上运行!要让它工作起来有点棘手,但有一个很好的例子

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

我在使用Procrun时遇到了一些困难,您的start和stop方法必须接受参数(String[]argv)。例如,“start(String[]argv)”和“stop(String[]argv)”可以工作,但“start()”和“stop()”会导致错误。如果不能修改这些调用,请考虑制作BooTrrasePrter类,可以根据需要调用这些调用。

< P>另一个答案是,这似乎是一个很好的替代java服务包装器,因为它有更好的许可。它还可以方便地从JSW移动到YAJSW。当然,对于我来说,windows服务器是一个全新的平台,并且试图让Java应用程序作为服务运行,这非常容易使用

我找到了其他一些,但最终没有使用:

  • 我没有使用这个,因为它看起来比YAJSW更复杂。我认为这不是包装纸
  • 创建Window的服务不是它的主要目标,而是。我没有使用这个,因为自2007年以来没有任何活动

不同解决方案的比较可以在以下位置找到:

我个人喜欢launch4j

使用“”,它是为Glassfish v3编写的,但通常与Java程序配合使用

需要安装.NET运行时。

现在,您可以使用自定义的可执行文件名称和图标您还可以使用自己的名称和图标获得自定义Windows托盘监视器

我现在使用自己的名称和图标(prunsrv.exe)运行服务,系统托盘监视器(prunmgr.exe)也有自己的自定义名称和图标

  • 下载(您将需要prunsrv.exe和prunmgr.exe)

  • 将它们分别重命名为
    MyServiceName.exe
    MyServiceNamew.exe

  • 下载并使用随附的
    RCEDIT.exe
    程序修改Apache可执行文件,以嵌入您自己的自定义图标,如下所示:

    > RCEDIT.exe /I MyServiceName.exe customIcon.ico
    > RCEDIT.exe /I MyServiceNamew.exe customTrayIcon.ico
    
  • 现在按如下方式安装Windows服务(有关更多详细信息和选项,请参阅):

  • 现在您有了一个Jar的Windows服务,它将使用您自己的图标和名称运行!您还可以启动监视器文件,它将以您自己的图标和名称在系统托盘中运行

    > MyServiceNamew.exe //MS//MyServiceName
    

  • 一个简单的方法是包装器()。

    Exe4j是一个非常好的选择,尽管它不是免费的。在创建.exe文件的向导中,您可以选择创建服务。

    这很简单,因为您必须输入快捷方式

    Windows7
    C:\users\All users\Start Menu\Programs\Startup
    (管理员)或
    用户主目录(%userProfile%)

    Windows 10: 在运行
    shell时:启动

    在它的属性->快捷方式->目标->
    java.exe-jard:\..\runJar.jar

    注意:这将仅在您登录后运行


    具有管理员权限

    sc create serviceName binpath=“java.exe-jar D:\..\runJar.jar”
    将创建windows服务

    如果您得到超时使用
    > MyServiceNamew.exe //MS//MyServiceName
    
    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
            }
        }
    
    }
    
    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"
    
    plugins {
      id "com.github.alexeylisyutenko.windows-service-plugin" version "1.1.0"
    }
    
    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'
    }
    
    -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.
    
    exe: Generates a Windows .exe package.
    msi: Generates a Windows Installer package.