Create a Windows Service for Java using WinRun4J

The following is a quick tutorial on a creating a windows service in Java using WinRun4J.

The first step is to download WinRun4J.

Now unzip it into any folder, create a new directory called service and copy over the following files from the download:

  • WinRun4Jc.exe
  • service.ini
  • WinRun4J.jar
  • WinRun4jTest.jar

Now rename WinRun4Jc.exe to service.exe and you should have a folder that looks like this:

Now we need a command prompt with Administrator priveleges. This can be created in Vista/Windows7 by going Start->All Programs->Accessories, right-click on Command Prompt and select Run as Administrator:

Now change into the service directory you created above:

To register the service with the Windows Service Control Manager (SCM), run the following command:

C:\downloads\service>service.exe --WinRun4J:RegisterService

Now if you start up the Windows service viewer you should see the WinRun4J test service:

You can start/stop the service using the service viewer.

The code listing for the service implementation is as follows:

package org.boris.winrun4j.test;

import org.boris.winrun4j.EventLog;
import org.boris.winrun4j.Service;
import org.boris.winrun4j.ServiceException;

/**
 * A basic service.
 */
public class ServiceTest implements Service {
    private int returnCode = 0;
    private volatile boolean shutdown = false;

    public int doRequest(int request) throws ServiceException {
        switch (request) {
        case SERVICE_CONTROL_STOP:
        case SERVICE_CONTROL_SHUTDOWN:
            shutdown = true;
            break;
        }
        return 0;
    }

    public int getControlsAccepted() {
        return SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
    }

    public String getName() {
        return "WinRun4J Test Service";
    }

    public String getDescription() {
        return "An example service using WinRun4J.";
    }

    public int main(String[] args) throws ServiceException {
        int count = 0;
        while (!shutdown) {
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
            }

            if (++count % 10 == 0)
                EventLog.report("WinRun4J Test Service", EventLog.INFORMATION,
                        "Ping");
        }

        return returnCode;
    }
}

The main method is called from the WinRun4J launcher when the service is required to run. This method should not return until the service is shutdown.

The doRequest method is called by the WinRun4J launcher on a separate thread (i.e. the service control thread). This method should modify the state of the application based on the request type passed in. In the example we just modify the shutdown flag.

The getName and getDescription methods are called when the service is registered. These values appear in the service viewer.

The example service simply logs to the windows event log every minute.

Advertisements

Create a Windows Executable for Java with Embedded JAR Files

The following is a quick tutorial on how to create a single executable for a java application using WinRun4J. We will also give it a splash screen.

To start download WinRun4J.

Now unzip it into any folder, create a new directory called winrun4j and copy:

  • WinRun4J.exe
  • WinRun4J.jar
  • WinRun4JTest.jar
  • WinRun4J.ini
  • SplashScreen.gif
  • RCEDIT.exe

You should now have a folder that looks like this:

Now create a console window and change directory to your winrun4j folder:

Now its time to use RCEDIT to embed the files into the WinRun4J executable. If you run RCEDIT you should see the following output:

F:\winrun4j>RCEDIT.exe
WinRun4J Resource Editor v1.0 (winrun4j.sf.net)

Edits resources in executables (EXE) and dynamic link-libraries (DLL).

RCEDIT   [resource]

  filename      Specifies the filename of the EXE/DLL.
  resource      Specifies the name of the resource to add to the EXE/DLL.
  /I            Set the icon as the default icon for the executable.
  /A            Adds an icon to the EXE/DLL.
  /N            Sets the INI file.
  /J            Adds a JAR file.
  /E            Extracts a JAR file from the EXE/DLL.
  /S            Sets the splash image.
  /H            Adds an HTML file the EXE/DLL.
  /C            Clears all resources from the EXE/DLL.
  /L            Lists the resources in the EXE/DLL.
  /P            Outputs the contents of the INI file in the EXE.

F:\winrun4j>

So to embed the JAR files we need to run the following command:

F:\winrun4j>RCEDIT.exe /J WinRun4J.exe WinRun4J.jar

And:

F:\winrun4j>RCEDIT.exe /J WinRun4J.exe WinRun4JTest.jar

Now we need to edit the WinRun4J.ini file to contain the necessary instructions (or in this case remove the unnecessary ones). After editing it should look like this:

main.class=org.boris.winrun4j.test.WinRunTest

Now we are ready to embed this file into the executable:

F:\winrun4j>RCEDIT.exe /N WinRun4J.exe WinRun4J.ini

The last thing we want to do is embed the splash screen:

F:\winrun4j>RCEDIT.exe /S WinRun4J.exe SplashScreen.gif

You should see that the WinRun4J.exe file has increased in size:

You should now be able to double click on the WinRun4J.exe and launch the java application:

To check that there are no magic tricks you can delete the other files (i.e. WinRun4J.jar, WinRun4J.ini, SplashScreen.gif, WinRun4JTest.jar and RCEDIT.exe). You only need the executable.

That’s all folks.

XLLoop 0.2.0 Released

A new release of XLLoop is now available (0.2.0). The new features and changes are:

  • Added popup to client for long running operations (with option to cancel request)
  • Added native Ruby server
  • Added native Python server
  • Added native R server
  • Fixed native Erlang server
  • Buffered streams on java server for improved performance
  • Improvements to reliability on binary protocol
  • Support up to 512 registered functions
  • Reduced xll size by using wcrt

Download at the XLLoop Sourceforge Site.

WinRun4J 0.3.0 Released

A new version of WinRun4J is now available (v0.3.0). The changes and new features are:

  • Added log.overwrite option for log files
  • Fixed log file logging
  • Added process.priority option to set process priority on startup
  • Fixed shutdown bug on service mode
  • Added ability to write text to splash screen
  • Fixes for splash screen loading in 64 bit mode
  • Improved window attributes on splash screen
  • Further improvements to diagnostics

Download at the WinRun4J Sourceforge Site.