java rmi - przyklad z ksiazki Java Techniki Zaawansowane

0

Siema,

mam problem z przykladem z ksiazki Java Techniki Zaawansowane. Ucze sie RMI i przepisalem wszystkie klasy dokladnie z ksiazki i zrobilem wszystko co tam napisano a i tak mam blad. Pomoze ktos i powie co robie zle? Chodzi chyba o rmiregistry.

Warehouse

 
import java.rmi.*;





public interface Warehouse extends Remote 

{

	double getPrice(String description) throws RemoteException;

}

WarehouseImpl

import java.rmi.*;

import java.rmi.server.*;

import java.util.*;





public class WarehouseImpl extends UnicastRemoteObject implements Warehouse 

{

	public WarehouseImpl() throws RemoteException

	{

		prices = new HashMap<String, Double>();

		prices.put("Blackwell Toaster", 24.95);

		prices.put("ZapXpress Micrwave Oven", 49.95);	

	}

	

	public double getPrice(String description) throws RemoteException

	{

		Double price = prices.get(description);

		return price == null ? 0 : price;

	}



	private Map<String, Double> prices;

} 

WarehouseServer

import java.rmi.*;

import javax.naming.*;





public class WarehouseServer 

{

	public static void main(String[] args) throws RemoteException, NamingException

	{

		System.out.println("Constructing server implementation... ");

		WarehouseImpl centralWarehouse = new WarehouseImpl();

		

		System.out.println("Binding server implementation to registry...");

		Context namingContext = new InitialContext();

		namingContext.bind("rmi:central_warehouse", centralWarehouse);

		

		System.out.println("Waiting for invocations from clients...");

	}

} 

WarehouseClient

import java.rmi.RemoteException;

import java.util.Enumeration;



import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NameClassPair;

import javax.naming.NamingException;





public class WarehouseClient 

{

	public static void main(String[] args) throws NamingException, RemoteException

	{

		Context namingContext = new InitialContext();

		

		System.out.println("RMI registry bindings: ");

		Enumeration<NameClassPair> e = namingContext.list("rmi://localhost/");

		while (e.hasMoreElements())

		{

			System.out.println(e.nextElement().getName());

		}

		String url = "rmi://localhost/central_warehouse";

		Warehouse cenWarehouse = (Warehouse) namingContext.lookup(url);

		

		String descr = "Blackwell Toaster";

		double price = cenWarehouse.getPrice(descr);

		System.out.println(descr + ": " + price);

	}

} 

Policy

grant 

{  

   permission java.net.SocketPermission 

      "*:1024-65535", "connect";

   permission java.awt.AWTPermission

      "accessEventQueue";

   permission java.net.SocketPermission 

      "localhost:80", "connect";

};
 

Potrzeba do tego jeszcze jakies NanoHTTPD.java, mozna sciagnac.

Najpierw w 1 konsoli odpalam to Nano, pozniej w drugiej poleceniem rmiregistry cos tam sie dzieje, chociaz nic nie wypisuje ani nic, kursor miga po prostu linie ponizej komendy, a nastepnie odpalam server i mam taki blad:

kod bledu

 java -Djava.rmi.server.codebase=http://localhost:8080/ WarehouseServer
Constructing server implementation... 
Binding server implementation to registry...
Exception in thread "main" javax.naming.CommunicationException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
	java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
	java.lang.ClassNotFoundException: Warehouse]
	at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:143)
	at com.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:226)
	at javax.naming.InitialContext.bind(InitialContext.java:417)
	at WarehouseServer.main(WarehouseServer.java:14)
Caused by: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
	java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
	java.lang.ClassNotFoundException: Warehouse
	at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:413)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
	at sun.rmi.transport.Transport$1.run(Transport.java:177)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:679)
	at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
	at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
	at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377)
	at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
	at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:137)
	... 3 more
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
	java.lang.ClassNotFoundException: Warehouse
	at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
	at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:403)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
	at sun.rmi.transport.Transport$1.run(Transport.java:177)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:679)
Caused by: java.lang.ClassNotFoundException: Warehouse
	at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:728)
	at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:672)
	at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:609)
	at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:646)
	at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311)
	at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:255)
	at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1548)
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1510)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1749)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
	... 12 more
 

Pomocy, bo nie moge ruszyc do przodu przez to.

0

A czy wystartowałeś rmiregistry z takiego miejsca w którym potrzebne klasy widać?

0

te pliki mam podzielone na foldery - download, server i client i w nich trzymam pliki .java i .class. To wszystko znajduje sie w folderze Warehouse i bedac w nim odpalilem rmiregistry, tak jak napisano w ksiazce, i nadal ten sam blad.

wlasciwie to w ksiazce jest napisane tak:

"Nastepnie otworz kolejne okno wiersza polecen, przejdz do katalogu, ktory nie zawiera zadnych plikow klasi i uruchom rejestr RMI: 
rmiregistry

Edit: Jak sie okazalo po odpaleniu rmi z katalogu server przeszlo, wiec problem sie rozwiazal:)

0
  1. Musisz utworzyc plik _stub.class : rmic WarehouseImpl,
  2. Odpalic rmiregistry,
  3. Kolejnym krokiem jest odpalenie serwera : java WarehouseServer
  4. Musisz odpalic klient java -Djava.security.policy=client.policy WarehouseClient

pisz jak bedziesz mial jeszcze problemu

0

@bmarcinster żartujesz prawda? rmica nie trzeba ręcznie używać od javy 1.4 bo teraz stuby generowane są "w locie". Problem autora był taki że odpalał rmiregistry z miejsca z którego nie było widać klas, classpath nie był dobrze ustawiony a remotowe repozytoria nie były zdeklarowane.

1 użytkowników online, w tym zalogowanych: 0, gości: 1