站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Compiling and Running the HelloWorld Example - JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Compiling and Running the HelloWorld Example


This document will show you how to compile and run a version of the "Hello World" example that was modified to install and use a custom RMI socket factory.


Compiling the Example

The first step is to create a directory to hold the source files, and to download the source files. If you haven't already completed this step, then please do so now.

For all of the source code used in this tutorial, you may choose from the following formats:

The directory that you create and download the source files into will be referred to as the SOURCE_DIRECTORY. So, if on your machine, you downloaded the source files into a directory named "test", then when following these instructions, you should substitute the word "test" wherever you see a reference to the "SOURCE_DIRECTORY".

  1. Create your SOURCE_DIRECTORY
  2. Download the source code into your SOURCE_DIRECTORY
  3. Change your directory to the SOURCE_DIRECTORY
  4. Extract the tarfile you downloaded
  5. Compile all the source files as follows:
  6. javac -d . *.java

    Because the files from this example all have the package name examples.rmisocfac, the results of the above command are:

    • A directory named examples, created in the SOURCE_DIRECTORY
    • A directory named rmisocfac, created in the directory examples
    • All the compiled .class files, placed in the directory rmisocfac
  • After compiling the source files, use the RMI compiler, rmic, to produce stub and skeleton code for the remote object implementation server, HelloImpl.
  • rmic must be invoked with the fully-qualified package name of the implementation class so in this example, the fully-qualified package name is examples.rmisocfac.HelloImpl. If you've followed the steps up until now, you should be sitting in your SOURCE_DIRECTORY, so to invoke rmic, type:

    rmic -d . examples.rmisocfac.HelloImpl

    When this command completes, the stub and skeleton should be located in the directory with the rest of the .class files.

    On Microsoft Windows platforms that directory would be, C:\SOURCE_DIRECTORY\examples\rmisocfac

    In the SolarisTM operating environment, that directory would be, ~/SOURCE_DIRECTORY/examples/rmisocfac

    After invoking rmic, the .class files for the stub and skeleton, HelloImpl_Stub.class and HelloImpl_Skel.class will be located in ~/SOURCE_DIRECTORY/examples/rmisocfac.
     


    Running the Example

    In this tutorial, the example will be run in three separate windows, or shells; one each for the rmiregistry, the client, and the server.
    1. In each window, change to the SOURCE_DIRECTORY (assuming the package directory, examples/rmisocfac is a subdirectory of the SOURCE_DIRECTORY).

    2.  
    3. Next, you need to make sure that the JavaTM 2 platform runtime knows where to find the executable files. To accomplish this, you'll need to set your CLASSPATH to include the SOURCE_DIRECTORY in all three windows.

    4.  
    Start the registry, then the server and then the client.
    1. To start the rmiregistry, choose one of the three windows as the registry window, and type:
    2. rmiregistry

      Note: Before you start the rmiregistry, you must make sure that the shell or window in which you will run the registry either has no CLASSPATH set or has a CLASSPATH that does not include the path to any classes that you want downloaded to your client, including the stubs for your remote object implementation classes.

      If you start the rmiregistry, and it can find your stub classes in its CLASSPATH, it will ignore the server's java.rmi.server.codebase property, and as a result, your client(s) will not be able to download the stub code for your remote object. For an explanation of how code downloading works in RMI, please take a look at the tutorial on Dynamic code downloading using RMI.

    3. To start the server, choose a window to be the server window. In that window, type:
    4. java -Djava.security.policy=/SOURCE_DIRECTORY/policy -Djava.rmi.server.codebase=file:/SOURCE_DIRECTORY/ examples.rmisocfac.HelloImpl

      You will know that the server started successfully when you see the message, "HelloServer Bound in registry." If you encounter errors starting the server, the likely problems will either be the CLASSPATH of the rmiregistry or that the java.rmi.server.codebase property was improperly set.
       

    5. To start the client, in the third window, type:
    6. java -Djava.security.policy=/SOURCE_DIRECTORY/policy examples.rmisocfac.HelloClient

      You will know that the HelloClient program completed successfully when the message "Hello World!" is printed to in that window. 



    Copyright © 1999 Sun Microsystems, Inc. All rights reserved.