Problem z uruchomieniem RMI

0

Witam!

Mam nastepujacy problem z uruchomieniem Java RMI.
Jest to najprostszy z mozliwych przykladow, jednak nie wiem czemu, ale nie moge utworzyc obiektu serwera, który posiada jedna metodę zdalna:

public String test()

.

Zacznijmy od systemu: uruchamiam moj program w Windows XP z Java 1.6.0, Netbeans 5.5.

Na poczatku uruchamiam z comandline rmiregistry:
start rmiregistry

Projekt serwera sklada sie z nastepujacych plikow:

- MainServerProject
   --- Iface.java ( klasa z interfejsem implementowanym przez serwer )
   --- MainServer.java ( klasa implementacji serwera )
   --- Main.java ( klasa z metoda main() )

Zawartosc pliku Iface.java:

public interface Iface extends Remote
{
    public String test() throws RemoteException;
}

Zawartosc pliku Mainserver.java:

public class MainServer extends UnicastRemoteObject implements Iface
{
    public MainServer() throws RemoteException
    {

    }

    public String test() throws RemoteException
    {
        return "wywolanie metody test";
    }

}

Zawartosc pliku Main.java:

public class Main
{

    public Main()
    {
    }

    public static void main(String[] args)
    {
       try
        {
            MainServer s2 = new MainServer();

            try
            {    
                Context namingContext = new InitialContext();

                //Naming.rebind( "s2", s2 );
                namingContext.bind( "s2", s2 );

            } 
            catch ( NamingException ex)
            {
                System.out.println("NamingException");
                ex.printStackTrace();
            }
            catch ( Exception ex)
            {
                System.out.println("Exception");
                ex.printStackTrace();
            }

        } 
        catch (RemoteException ex)
        {
            ex.printStackTrace();
        }

    }

}

Problemy:
1) w czasie uruchomienia. gdy uzywam metody

bind

generowany jest wyjatek:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.bind(InitialContext.java:400)
at serversite.Main.main(Main.java:34)

2) w czasie uruchomienia. gdy uzywam metody

rebind

generowany jest wyjatek:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: serversite.Iface
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at serversite.Main.main(Main.java:33)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: serversite.Iface
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: serversite.Iface
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:711)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:655)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:592)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1531)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1493)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
... 12 more

Spedzilem duzo czasu nad tym i nie wiem w czym blad :-(

Nie wiem takze, jaka jest roznica miedzy metoda

bind

a rebind


.

Prosze o pomoc.
Pozdrawiam
0

Jeśli coś nie działa a masz o tym nikłe pojęcie to najlepiej zrobisz jeśli weźmiesz gotowca :
http://java.sun.com/docs/books/tutorial/rmi/index.html

PS: Pamiętaj o tym że do kompilacji potrzebny jest rmic a nie javac .
Oraz że pieniek (ang. stub) serwera musi być dostępny dla klienta.

0

Juz mniej wiecej wiem w czym byl problem - nie zostala ustawiona zmienna CLASSPATH.
Jezeli korzystalem do tej pory z netbeansa to niewiele musialem sie nia przejmowac, natomiast w momencie kiedy uruchamiam start rmiregistry to w jakims celu potrzebna jest sciezka do mojego projektu, w ktorym zdefiniowany jest inteface.
Zatem po ustawieniu sciezki CLASSPATH w moim przypadku na:
.;D:\Projects\SR\MainServerProject\dist\MainServerProject.jar
serwer zaczal sie uruchamiac (jednak tylko w przypadku uzycia metody rebind). Rowniez klient dziala bez problemow:

public class Client
{

    public Client()
    {
    }

    public void showBidings()
    {
        try
        {
            Context contextName = new InitialContext();

            NamingEnumeration<NameClassPair> e = contextName.list( "rmi:" );

            System.out.println("RMI:");
            while( e.hasMore() )
            {
                System.out.println( e.next().getName() );
            }

            e = contextName.list( "rmi://localhost/" );

            System.out.println("RMI://localhost");
            while( e.hasMore() )
            {
                System.out.println( e.next().getName() );
            }
        } 
        catch (NamingException ex)
        {
            ex.printStackTrace();
        }
    }

    void runRemoteMethod()
    {
       System.setProperty( "java.security.policy", "client.policy" );
       System.setSecurityManager( new RMISecurityManager() );

        try
        {
            Context contextName = new InitialContext();

            String url = "rmi://localhost/";
            Iface server = (Iface)( contextName.lookup( url + "s2" ) );
            System.out.println( server.test() );

        } 
        catch ( Exception ex)
        {
            ex.printStackTrace();
            System.out.println( ex.getMessage() );
        }
    }

}

Jezeli bede wiedzial jak zmusic do dzialania metode bind to postaram sie to opisac.

Pozdrawiam

0

a sprawdzałeś bind po restarcie komputera ?

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