Pastebin

Paste #181:

< previous paste - next paste>

Pasted by tdn

Download View as text

Jeg gør følgende:

  1) Starter rmiregistry: 
       start rmiregistry 2001

  2) Start Server:
C:\eclipse-SDK-3.2-win32\workspace\RMISamples\src>tree /f /a
Mappetræ for diskenheden IBM_PRELOAD
Diskenhedens serienummer er 348B-FB89
C:.
\---example
    \---hello
            Client.java
            Hello.java
            Server.java


C:\eclipse-SDK-3.2-win32\workspace\RMISamples\src>java -classpath example/hello
example.hello.Server
Exception in thread "main" java.lang.NoClassDefFoundError: example/hello/Server

C:\eclipse-SDK-3.2-win32\workspace\RMISamples\src>java -classpath . example.hell
o.Server
Exception in thread "main" java.lang.NoClassDefFoundError: example/hello/Server

C:\eclipse-SDK-3.2-win32\workspace\RMISamples\src>

Source code herunder 
=======================================================


Hello.java:
package example.hello;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}



Server.java:
package example.hello;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello {

	public Server() {
	}

	public String sayHello() {
		return "Hello, world!";
	}

	public static void main(String args[]) {

		try {
			/*
			 * The main method of the server needs to create the remote object that 
			 * provides the service. Additionally, the remote object must be 
			 * exported to the Java RMI runtime so that it may receive incoming 
			 * remote calls. This can be done as follows:
			 */
			Server obj = new Server();
			int port = 2002;
			Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, port);

			// Bind the remote object's stub in the registry
			Registry registry = LocateRegistry.getRegistry(2001);
			registry.bind("Hello", stub);

			System.err.println("Server ready");
		} catch (Exception e) {
			System.err.println("Server exception: " + e.toString());
			e.printStackTrace();
		}
	}
}




Client.java:
package example.hello;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {

	private Client() {
	}

	public static void main(String[] args) {

		int port = 2001;
		String host = (args.length < 1) ? null : args[0];
		try {
			Registry registry = LocateRegistry.getRegistry(host, port);
			Hello stub = (Hello) registry.lookup("Hello");
			String response = stub.sayHello();
			System.out.println("response: " + response);
		} catch (Exception e) {
			System.err.println("Client exception: " + e.toString());
			e.printStackTrace();
		}
	}
}



New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.