Turn a Java program into a Windows system service

Source: Internet
Author: User

I recently bought a machine as a source server and a few friends from other places to do something to play. However, due to the use of China Telecom's ADSL, the address is dynamic and changes every day when the machine restarts automatically. So I bound it with a 3322 domain name, but the windows Update dns client provided by 3322 does not run as a system service, so it does not run when no user logs on, so the problem persists. Later, I got a Windows Resource Kit Tools tool (this tool is not available for windows server by default. It needs to be downloaded from Microsoft's website. I didn't remember the specific address, but google will certainly find it ), there are some tools that can turn common win32 programs into system services.

Instsrv: this tool turns win32 programs into system services, but not all programs are suitable for system services. For example, a CN99 client cannot be started after being registered as a system service.

Basic usage: instsrv [service name] [absolute Application Path]

Srvany: this tool is used to solve the problem in the above tool. When you register it, you regard it as the service you want and use it to start your application.

Instsrv [service name] [absolute path of srvany]

After successful registration, go to the service manager and open the service you just registered. Fill in the absolute path of your application in the startup parameter to start the service.

This method can solve the problem of updating the domain name address.

But I am not happy. As a programmer, it is always uncomfortable to be so constrained by people. In fact, the CN99 dynamic domain name update protocol is very simple, and a simple program is implemented. Instead, we can only use Java. For C or C ++ programmers, it is really a small Case to make a system service. I regret that I didn't learn C and C ++ well at the beginning. But is there no way to turn a Java program into a windows system service? In the world of Java, there is basically no way to do it. The ObjectWeb Forge project called assumervice is used to register a Java program as a system service. Let's take a look:

A try -- failed

Test 2-unsuccessful

Try three --...

...
...

Give up!

I did it myself. In fact, with the above instsrv and srvany, there is actually an executable file. So I used JBuilder to make my jar package into a fake exe file.

My DNS update program is as follows:
Package org. mstar. dns;

Import java. util. TimerTask;
Import org. apache. commons. httpclient. HttpClient;
Import org. apache. commons. httpclient. methods. GetMethod;
Import org. apache. commons. httpclient. HttpStatus;
Import org. apache. commons. httpclient. HttpException;
Import java. io. IOException;
Import java. util .*;
Import org. apache. commons. logging .*;
Import org. apache. commons. httpclient. Credentials;
Import org. apache. commons. httpclient. UsernamePasswordCredentials;
Import org. apache. commons. httpclient. auth. AuthScope;
Import java. text. SimpleDateFormat;

Public class Refresher {
Private static Log LOG = LogFactory. getLog (Refresher. class );
Public Refresher (){
Timer timer = new Timer ();
Timer. schedule (new RefreshTask (), 0, 60000 );
}

Class RefreshTask extends TimerTask {
Static final String URL = "http://members.3322.org/dyndns/update? System = dyndns & hostname = zjumty.3322.org ";

Public void run (){
HttpClient client = new HttpClient ();
Client. getParams (). setAuthenticationPreemptive (true );
Credentials defacreds = new UsernamePasswordCredentials ("username", "password ");
// Here I want to keep it confidential
Client. getState (). setCredentials (new AuthScope ("members.3322.org ",
80, AuthScope. ANY_REALM), defaultcreds );
GetMethod method = new GetMethod (URL );
Try {
Int statusCode = client.exe cuteMethod (method );
If (statusCode! = HttpStatus. SC _ OK ){
LOG. error ("Method failed:" + method. getStatusLine ());
}
Date date = Calendar. getInstance (). getTime ();
SimpleDateFormat fmt = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss ");
Byte [] responseBody = method. getResponseBody ();
LOG.info (fmt. format (date) + "--" + new String (responseBody ));
} Catch (HttpException e ){
LOG. error ("Fatal protocol violation:" + e. getMessage ());
E. printStackTrace ();
} Catch (IOException e ){
LOG. error ("Fatal transport error:" + e. getMessage ());
E. printStackTrace ();
} Finally {
Method. releaseConnection ();
}
}
}

Public static void main (String [] args ){
Refresher refresher = new Refresher ();
}
}

In fact, we can use the HttpClient of Jakarta to request a URL.

Now I have my refresher.exe. Of course, your system must have a JVM to run (nonsense ). For ease of running, I put the jar package at runtime, such as logger. jar and httpclient. jar, in my jar package.

The next step is to make it a system service.

Step 1: instsrv Refrehser C: Program FilesWindows Resource KitsToolssrvany.exe

Step 2: regedit open the Registry

Step 3: Go to HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesRefrehser

Step 4: Right-click "new" and enter "Parameters" as the item.

Step 5: open Parameters, right-click-New-string value Application

Step 6: Double-click Application and enter c: efresher.exe.

Step 7: Start the Refresher service.

Everything is OK.

You can also add many key values in Parameters:

AppDirectory working directory

AppParameters parameters of your application. (I can make the above program an acceptable parameter, so it is more common)

AppEnvironment running environment variable.

Now, as a Java programmer, you can add some kinetic energy to your system. Please do it now!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.