New Version of WinRun4J (0.4.3) Available

Head over to the WinRun4J Sourceforge Page to download.

This includes the following fixes/improvements:

  • Fixed loading issue with Java 7
  • Added a standalone java service wrapper

Edit: Also note that the source code for WinRun4J has now moved to GitHub. Head over to the WinRun4J Github Page to view/download/fork.

A new version of WinRun4J is available. …

A new version of WinRun4J is available. It contains the following fixes and new features:

  • Major new feature is the dynamic native binding. This provides the ability to use native Windows API functions without having to write any JNI/native code. See native binding examples for more information.
  • Fixed a race condition in the service implementation for quick starting applications.
  • The launcher java library now requires java 1.5 minimum (due to use of annotations). The launcher executable is compatible with java 1.4 and above.
  • Fixed lowercased key issue with INI file
  • Fixed max heap size issue on 64-bit VM
  • Added option to set console title via INI file
  • Added option to suppress error popups
  • DDE activate message sends command line

The following shows a simple example of the native binding:

package org.boris.winrun4j.test;

import org.boris.winrun4j.PInvoke;
import org.boris.winrun4j.PInvoke.DllImport;
import org.boris.winrun4j.PInvoke.UIntPtr;

public class BindingExample1
{
    @DllImport("kernel32")
    public static native boolean GetComputerName(StringBuilder lpBuffer, UIntPtr lpnSize);
    
    public static void main(String[] args) throws Exception {
        StringBuilder name = new StringBuilder();
        UIntPtr size = new UIntPtr(100);
        if (GetComputerName(name, size)) {
            System.out.println(name);
        }
    }

    static {
        PInvoke.bind(BindingExample1.class);
    }
}

You can download the new version from the WinRun4J Sourceforge Site.

Thanks to everyone who found bugs and gave suggestions.

WinRun4J 0.3.3 Released

A new version of WinRun4J is available. It contains the following fixes and new features:

  • Moved service name, description and controls accepted to INI file
  • Refactored Service interface and added AbstractService helper class
  • Implemented FileAssociations helper class
  • Greatly improved the Registry API
  • Option to log to console and file
  • Log file rolling
  • Fix for relative log file path

The Registry API has been refactored to be easier to use (no need to open/close).

The Service API is now also much easier to use, with an AbstractService class. This makes the example service implementation much simpler:

package org.boris.winrun4j.test;

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

/**
 * A basic service.
 */
public class ServiceTest extends AbstractService
{
    public int serviceMain(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 0;
    }
}

The shutdown variable is set to true when the service is requested to close by the service control manager.

You can download the new version from the WinRun4J Sourceforge Site.

Twitter Backup Utility

The following is a simple tool I hacked together in Java that downloads your twitter statuses to a directory, to test out the new WinRun4J Plugin for Eclipse. I wrote a simple Java class and then exported it to a single fat executable (console version): TwitterBackup.exe.

Note that you need Java to run this application: You can download it from here.

If you run it without command line arguments you get:

Twitter Backup v0.1.0 @winrun4j

A backup utility for Twitter statuses.

TwitterBackup [-user <user>|-search <search>] -outdir <backup_dir> 
    [-limit #hours|day|week|month|year]

The -user option is simply your twitter user id.

The -search option is a search query (you probably want to put “quotes” around it).

The -outdir option is the directory you want the statuses saved into.

The -limit option sets a time/date limit on the history, so “day” only loads statuses for the last 24 hours. The #hours is a number of hours (e.g. -limit 4 only saves statuses from the last 4 hours).

Some examples:

TwitterBackup -user twitter -outdir C:/TEMP -limit 12

This will save the “twitter” user’s statuses from the last 12 hours to the directory C:/TEMP.

TwitterBackup -search "cool filter:links" -outdir C:/TEMP -limit day

This will save all search results for “cool filter:links” for the last 24 hours to the directory C:/TEMP.

The utility is also incremental so you can run it over and over again and it will only grab new statuses.

It is designed to be run as a scheduled task (e.g. setup a schedule task to run every week and use the -limit week option).

Each twitter status is saved into a separate file using the unique status id provided by twitter. It simply contains the RSS item XML.

You can download TwitterBackup here

Note that Twitter has limits on how much you can download at one time and how far back in time you can go.

BTW The utility should work as specified but the author accepts no responsibilities is something screws up. Use at your own risk.

Launcher File Double-Click in Explorer

The following is a potentially useful hack if you have plenty of different java apps you need to launch from explorer but don’t want to create separate launcher files for each launcher configuration file.

Using Windows file associations we create a new file association for *.winrun4j to the WinRun4J launcher. The command executed is the new WinRun4J --WinRun4J:ExecuteINI command added in the previous release.

The following registry script updates the appropriate entries:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.winrun4j]
@="WinRun4JLaunch"
"PerceivedType"="text"

[HKEY_CLASSES_ROOT\.winrun4j\OpenWithList]

[HKEY_CLASSES_ROOT\.winrun4j\OpenWithList\WinRun4J.exe]

[HKEY_CLASSES_ROOT\WinRun4JLaunch]
@="WinRun4J Launcher Configuration"

[HKEY_CLASSES_ROOT\WinRun4JLaunch\DefaultIcon]
@="F:\\Development\\tools\\WinRun4J.exe"

[HKEY_CLASSES_ROOT\WinRun4JLaunch\shell]

[HKEY_CLASSES_ROOT\WinRun4JLaunch\shell\open]

[HKEY_CLASSES_ROOT\WinRun4JLaunch\shell\open\command]
@="F:\\Development\\tools\\WinRun4J.exe --WinRun4J:ExecuteINI %1"

This assumes that you have the latest version of the launcher saved into F:/Development/tools – if not just change the above entries to point to your folder.

The following screenshot shows a bunch of launcher files:

Now you can create any launch configuration and give it a .winrun4j extension and then double-click to launch.

Caveat: As per usual, make sure you backup your registry before modifying.

New Eclipse Plugin for WinRun4J

The initial version of the Eclipse Plugin for WinRun4J is now available from the WinRun4J SourceForge site.

The Update Site URL is: http://winrun4j.sourceforge.net

The main features of the addin are (as per the website):

  • Launch any Java Application with WinRun4J (via right-click or launch configuration)
  • Export your Java Application as a single Windows Executable (with necessary jars embedded)
  • Provides improved windows integration from Eclipse (eg. JNI access to Registry).

To install the plugin in eclipse go to Help->Software Updates… then select Add Site… then
add http://winrun4j.sourceforge.net and click OK. The select “WinRun4J Eclipse Plugin 0.1.0” and click Install… and follow the prompts.

Further help on using the plugin is on the plugin website

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.