Overview |
As with most applications and development tools, the best place to start is usually with a simple Hello World! example. The usual Java Hello World application starts up, prints out Hello World and then exits. That is not very interesting to show off a tool for running and monitoring services. In the case of the Java Service Wrapper, an example that can be installed and then stay running as a Windows Service or UNIX daemon is required. This page will describe our Hello World Server example application and help you through the process of building it. The How can I run my Java application as a Windows Service? page will make use of this example to walk you through the process of quickly configuring and installing this example to run as a Windows Service with the Java Service Wrapper. This example Hello World application launches a daemon thread, which listens on port 9000 for simple text based clients, who connect through telnet. It will then simply echo any input until it finds the input exit, at which point that particular connection will be closed. While this is a little more complicated than the typical Hello World example, it allows you to run an easy server application, being able to interact with a client program, following the fundamental server client design model. |
Example Source Code |
||||
To keep our example clean and let it work cleanly with our examples, please start with an empty directory at the location of your choice that we will call %HELLO_HOME%. Please create lib\classes and src\java directories within %HELLO_HOME%. You should end up with the following:
Next create a single file in the src\java directory
called HelloWorldServer.
The application starts up and begins listening on port 9000 for remote connections from any source. As each new connection is accepted, a dedicated handler thread is created to handle all communication with that connection until it is closed. The handler thread will read and then echo any line of text back to the client. If a line of text containing only the word exit is received, then the client connection will be closed. |
Building the Example |
||||
Building the example is a simple task. Start by opening up a Command Prompt or Shell on UNIX and go into the %HELLO_HOME% directory, which we created above, using the cd command. The example can now be compiled using the following command:
If you get an error that the javac command could not be found, please make sure that you have installed a Java SDK on your system. If everything went well, then you should get the prompt back without any additional output after the compiling is done. Please check to make sure that the class file was created by running:
Congratulations, you have successfully built the HelloWorldServer example. |
Running the Example |
||||||||||||||
Running the example can be done executing the following command from within the %HELLO_HOME% directory.
If everything is working, you should see the following:
|