prunsvc can't run java executable as service on Windows 8.1

803 Views Asked by At

I have converted a java executable jar to a Windows executable using Launch4j. I have installed it and a jre to a Windows 8 box, in the c:\program files (x86)\mycomany directory, and executed the following as administrator in a bat file (in order to use prunsvc to run the application as a service):

    rem determine the full path to this bat file
    set thispath=%~dp0

    set SERVICE_NAME=MyService
    set PR_INSTALL=%thispath%prunsrv.exe

    REM set service stdout and stderr
    set PR_LOGPATH=%thispath%
    set PR_STDOUTPUT=%thispath%stdout.txt
    set PR_STDERROR=%thispath%stderr.txt
    set PR_LOGLEVEL=Error

    REM path to java
    set PR_JVM="%thispath%jre1.8.0_45\bin\client\jvm.dll"
    set PR_CLASSPATH="%thispath%jre1.8.0_45\bin\MyService.exe"

    REM Startup configuration
    set PR_STARTUP=auto
    set PR_STARTMODE=jvm
    set PR_STARTCLASS=com.mycompany.mainclass
    set PR_STARTMETHOD=start

    REM Shutdown configuration
    set PR_STOPMODE=jvm
    set PR_STOPCLASS=com.mycompany.mainclass
    set PR_STOPMETHOD=stop

    REM JVM configuration
    set PR_JVMMS=256
    set PR_JVMMX=1024
    set PR_JVMSS=4000
    set PR_JVMOPTIONS=-Duser.language=EN;-Duser.region=en

    set PR_DESCRIPTION="My Service Description"

    "%PR_INSTALL%" install %SERVICE_NAME% 

The service appears to install successfully; however when I do:

prunsrv.exe start myservice

at the command line as administrator, nothing happens. Instead I see the following in the Windows event log:

The MyService service terminated with the following service-specific error: Incorrect function.

2

There are 2 best solutions below

0
On

Incorrect function - means you did not implement method "stop" since the setting was set PR_STOPMETHOD=stop. So, if you haven't implemented a stop method or it is not static function. If you want to use the "main" instead then leave this setting. and set PR_STOPPARAMS=stop and implement in your main the argument "stop" string to stop the service.

0
On

The "incorrect function" in conjunction with Java generally indicates a 32 bit / 64 bit mismatch between the used JVM and the invoking service.

Using prunsrv to create a service explains the path to success and chapter 8 tells you all the possible reasons why you might get the dreaded "error: Incorrect function". So make sure that:

  • prunsrv.exe is installed for the correct architecture: 32 or 64 bit depending on Windows architecture
  • when running the command line, path and options containing spaces are double-quoted
  • the JVM parameter refer to the correct JDK/JRE installation
  • the logfiles folder, stdout.out and stderror.out logfiles must be accessible for writing