In one of the previous blog-articles a colleague of mine described how to start an OC4J-container on the command line. Which was and is correct, butâ€¦ Iâ€™m a database administrator and – Sorry Rob ;-)) we donâ€™t like to leave machines with user – or even worse â€“ administrative accounts still logged on to production systems. So, here comes a description how to start/stop OC4J-standalone as a Windows service using a tool called JavaService.exe (to download via object.org).
This executable works on (the 32-bit versions of) Windows NT, 2000, XP or 2003 (NOT on 95,98 or ME).

First of all download the JavaService.zip which contains the executable which later runs the service just like the beter known srvany.exe does for â€œnormalâ€ Windows programâ€™s.

Then make a copy of the executable and give it a name like anyway_you_like.exe, just choose a name which can easily be associated with the application you want to run on the container or the container itself. This changed name just makes it easier to recognize your executable later in the process list of the server, in case you have to troubleshoot the server (for example for memory leaks). And you can run many different containers on one machine with different names without mixing them up.

The main part of this conversion into a Windows service are two command-files:

The first one is to install the service itself; it contains all parameters and settings to install and run the service and the oc4j-container the way you want him to.

The second is more or less an uninstall-script. It is used whenever you want to change the starting conditions of the service. You have to de-install the service first, before installing it anew with its new settings (which takes about 5 seconds of your life).

So, what does the â€œInstall_my_OC4J_container_service.cmdâ€ look like?

Here is the code:

setlocal
@rem Note that if JVM not found, service 'does not report an error' when startup fails, although event is logged
if %JAVA_HOME% == set JAVA_HOME=c:\path\to\
REM  set the display name of the service
set JSNAME=OC4J_Service
REM Set the Java server directory
set JVMDIR=%JAVA_HOME%\jre\bin\server

REM Setting a bind directory for the software
set JSBINDIR=%CD%
REM Setting path and filename of the (renamed) javaservice executable set JSEXE=%JSBINDIR%\OC4J_Service.exe
REM Stating the directory of the OC4J-home
set OC4J_HOME=D:\testinstall\oc4j\j2ee\home
REM just to check the used version @echo . To install the service â€œ%JSNAME%â€ the following version of JavaService executable is used: @ echo . %JSEXE% -version @echo .
@echo Installing service OC4J_Service ... Press Enter to continu or press Control-C to abort installation
@echo
%JSEXE% -install %JSNAME% %JVMDIR%\jvm.dll -Djava.class.path=%OC4J_HOME%\oc4j.jar -start com.evermind.server.OC4JServer -params -config %OC4J_HOME%\config\server.xml -out %OC4J_HOME%\log\OC4J_service_stdout.log -err %OC4J_HOME%\log\OC4J_service_stderr.log -current %JSBINDIR% -manual -description â€œOC4JService"
@echo
@echo End of Installation
@pause

The main line starts with â€œ>%JSEXE% -install â€¦. Here all the parameter must be on one line or at least consecutively stated.

Letâ€™s have a look wat this one does:
% -install %JSNAME% : calls the (renamed) javaservice.exe (in its directory somewhere on the machine), adds the install switch and passes the display name of the new service to the operating system

%JVMDIR%\jvm.dll : tells Windows which dll to use to run the service instead of the normal call to java.exe on the command line. This dll should be in the JRE or if is not present download it from for example SUN or a site like dlldump.

-Djava.class.path=%OC4J_HOME%\oc4j.jar : sets the classpath of the oc4j.jar just as in the known java â€“jar oc4j.jar command when the call comes from a directory which is not the â€œthe directory of the calling programâ€.

-start com.evermind.server.OC4JServer : this parameter tells the dll what to actually start. In this case it starts a OC4J 10.1.2 container. For OC4J 10.1.3 use -start oracle.oc4j.loader.boot.BootStrap

-params : Meaning: Attention DLL! Startup Parameters will followâ€¦

-config %OC4J_HOME%\config\server.xml : â€¦ use the server.xml of the container as your config-file

-out %OC4J_HOME%\log\OC4J_service_stdout.log : this is the path to the log-file as a standard out-file

-err %OC4J_HOME%\log\OC4J_service_stderr.log : the definition of the error-file

-current %JSBINDIR% : tell me where the javaservice.exe is

-manual : Startup option of the Windows service

-description â€œOC4JService”; : The description line of the service

And where do I put my java heap size or the instructions to run in debug-mode etc?

Somewhere between -D… and -start.

Put the parameters about het heap size after “oc4j.jar” and the debugging should come before the call of this jar-file.

So, to force the above service to start with a maximum of 256M heap size and in debug mode for JDBC the whole line becomes:

%JSEXE% -install %JSNAME% %JVMDIR%\jvm.dll -Djdbc.debug=true java.class.path=%OC4J_HOME%\oc4j.jar â€“Xmx256M -start com.evermind.server.OC4JServer -params -config %OC4J_HOME%\config\server.xml -out %OC4J_HOME%\log\OC4J_service_stdout.log -err %OC4J_HOME%\log\OC4J_service_stderr.log -current %JSBINDIR% -manual -description â€œOC4JService”

And what does the uninstall file say?

@echo off
@echo . Uninstall_OC4JService.cmd - de-install of JavaService executable using OC4J.jar
@echo .
setlocal
@rem Note that if JVM not found, service 'does not report an error' when startup fails, although event logged
if "%JAVA_HOME%" == "" set JAVA_HOME=c:\java\j2sdk142
set JVMDIR=%JAVA_HOME%\jre\bin\server
set JSBINDIR=%CD%
set JSEXE=%JSBINDIR%\OC4J_Service.exe
@echo . Using following version of JavaService executable:
@echo .
%JSEXE% -version
@echo .
@echo Un-installing service OC4JService ... Press Control-C to abort
@pause
@echo .
%JSEXE% -uninstall OC4JService
@echo .
@echo End of script
@pause

It is starting like the install command script, setting all parameters but the main command just says:

%JSEXE% -uninstall OC4JService

or in human terms: Take the javaservice.exe and uninstall the given service name.

Now your install file is ready to be executed and if something goes wrong just de-install it again untill everything is working.

Note: If you like to use Service names with spaces, donâ€™t forget to use the dubble quotes around the name in the commands.

And just one last tip: To prevent an accidential un-install store your uninstall.cmd-file as a renamed version, something like uninstall.cmd.rename_me_before_use.

Share.

1. Tim Melander on

To fix the problem dealing with spaces in the path the following can be used to enhance the install bat file.

The following can be added just before the “set JVMDIR=%JAVA_HOME%\jre\bin\server” line.

set J2EE_HOME=%1
SET J2EE_HOME=###%J2EE_HOME%###
SET J2EE_HOME=%J2EE_HOME:”###=%
SET J2EE_HOME=%J2EE_HOME:###”=%
SET J2EE_HOME=%J2EE_HOME:###=%
set OC4J_HOME=%2\j2ee\home
set JVMDIR=%J2EE_HOME%\jre\bin\%3