Results 1 to 18 of 18
  1. #1
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Java RMI Problem

    Hello everyone :)

    I am currently trying to make a Chat Client/Server application in java using RMI.

    I have a bit of a problem, I am using NetBeans.

    Java Code:
    import java.rmi.*;
    import java.rmi.server.RemoteServer;
    
    public class RMIClient {
    
        public static void main(String[] args) {
            try
            {
                RemoteServer CS=(RemoteServer) Naming.lookup("rmi://localhost/ChatServer");
                CS.helloWorld();
            }
            catch ( Exception e )
            {
                System.out.println("Exception: " + e.getMessage());
            }
        }
    
    }
    Thats the client code and I get the following error message when i run it. The server is running fine.

    run:
    Exception: $Proxy0 cannot be cast to java.rmi.server.RemoteServer
    BUILD SUCCESSFUL (total time: 0 seconds)

    This is an example code from my tutor and it worked when i was using the uni computers but i can't re-create it on my own laptop.

    The laptop is running Windows Vista Home Premium if thats any help.

    Any ideas?

    Thanks.

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    did u create the required stubs for server code?

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    What is your server code?

  4. #4
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    import java.rmi.*;
    import java.rmi.server.*;
    
    public class RMIServer extends java.rmi.server.UnicastRemoteObject
            implements RemoteServer
    {
        public RMIServer() throws java.rmi.RemoteException
        {
        }
    
        public void helloWorld() throws java.rmi.RemoteException
        {
            System.out.println("hello world");
        }
    
        public static void main(String args[]) throws Exception
        {
    	RMIServer CS=new RMIServer();
    	Naming.rebind("rmi://localhost/ChatServer",CS);
        }
    }
    Thats the server code.

    Im honestly new to Java, I joined the 3rd year of a degree in a different uni and I have never done Java before so im a bit confused by it all.

  5. #5
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    I just tried to create the Stubs using some extra documents i found and got this error.

    C:\Users\Chris\Documents\NetBeansProjects\RMIServe rClient\build\classes>C:\"Prog
    ram Files"\Java\jdk1.6.0_11\bin\rmic RMIClient
    error: Class RMIClient does not implement an interface that extends java.rmi.Remote; only remote objects need stubs and skeletons.
    1 error

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    The above can not be the complete server code, the very first thing what you need in a rmi application is an interface that extends RemoteInterface, see if the following works

    RemoteServer.java
    --------------------
    import java.rmi.*;
    import java.rmi.server.*;

    public interface RemoteServer extends Remote{
    public void helloWorld() throws java.rmi.RemoteException;
    }

    RMIServer.java
    --------------
    import java.rmi.*;
    import java.rmi.server.*;

    public class RMIServer extends java.rmi.server.UnicastRemoteObject
    implements RemoteServer
    {
    public RMIServer() throws java.rmi.RemoteException
    {
    }

    public void helloWorld() throws java.rmi.RemoteException
    {
    System.out.println("hello world");
    }

    public static void main(String args[]) throws Exception
    {
    RMIServer CS=new RMIServer();
    Naming.rebind("rmi://localhost/ChatServer",CS);
    }
    }

    steps:
    1.Now compile both the codes
    2. Generate the stubs type the following command prompt in the same way as you do javac
    rmic RMIServer
    3. Start the RMI registry, type the following command prompt from command prompt
    start rmiregistry
    4. Execute your server program i.e. java RMIServer
    5. Execute your client program.

  7. #7
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks for the help.

    I did that and i made the stubs for the Server and tried to run it.

    But i get a very big error now.

    Exception in thread "main" java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: java.lang.ClassNotFoundException: RMIServer_Stub at sun.rmi.server.UnicastServerRef.oldDispatch(Unicas tServerRef.java:396)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastSe rverRef.java:250)
    at sun.rmi.transport.Transport$1.run(Transport.java:1 59)
    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:5
    35) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run0(TCPTranspor
    t.java:790) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run(TCPTransport
    .java:649) at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExec
    utor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor
    .java:908) at java.lang.Thread.run(Thread.java:619) at sun.rmi.transport.StreamRemoteCall.exceptionReceiv edFromServer(Unknow
    n Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unk nown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Unknown Source)
    at RMIServer.main(RMIServer.java:19)
    Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
    ception is:
    java.lang.ClassNotFoundException: RMIServer_Stub
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknow n Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(Unicas tServerRef.java:386
    )
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastSe rverRef.java:250)
    at sun.rmi.transport.Transport$1.run(Transport.java:1 59)
    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:5
    35)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run0(TCPTranspor
    t.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run(TCPTransport
    .java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExec
    utor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor
    .java:908)
    at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.ClassNotFoundException: RMIServer_Stub
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandl er.java:434)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandl er.java:165)
    at java.rmi.server.RMIClassLoader$2.loadClass(RMIClas sLoader.java:620)
    at java.rmi.server.RMIClassLoader.loadClass(RMIClassL oader.java:247)
    at sun.rmi.server.MarshalInputStream.resolveClass(Mar shalInputStream.jav
    a:197)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectI nputStream.java:157
    5)
    at java.io.ObjectInputStream.readClassDesc(ObjectInpu tStream.java:1496)
    at java.io.ObjectInputStream.readOrdinaryObject(Objec tInputStream.java:1
    732)
    at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1329)
    at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:351)
    ... 12 more

  8. #8
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Some thing seems wrong with you classpath setting, copy your all codes to bin folder of jdk and see if it works from there.

  9. #9
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Most likely a different rmiregistry might be of oracle (if installed) or something else is being used when you start rmiregistry, executing your codes from bin directory should resolve your problem for the time being.

  10. #10
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    I just tried that and I get the same error as above.

    I tried running it from the bin folder with both:
    Java.exe RMIServer and Java RMIServer but got the same error on both :(

  11. #11
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    I just figured it out, The rmiregistry that was running was the one from JRE not the JDK version. When i loaded the JDK rmiregistry in a command window it started to work.

    I get this error when running the client:

    run:
    Exception: error unmarshalling return; nested exception is:
    java.lang.ClassNotFoundException: RMIServer_Stub (no security manager: RMI class loader disabled)
    BUILD SUCCESSFUL (total time: 0 seconds)
    lol, im such a pain...

  12. #12
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    The nature of problem appears to be same, as I said try to execute from jdk\bin and see if it works.

  13. #13
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    I just tried that and I get the same error. Just a note that the client isn't in the same project as the server if that would make any difference.

  14. #14
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Right a little update

    I made an RMIClient Class in the same project as the Server and copied the code over.

    I tried to run it through the command window and got the following error:

    "Exception: Connection refused to host: 192.168.1.103; nested exception is: java.net.ConnectException: Connection refused: connect"

    I can only assume this is something on my PC like a firewall or something, so I am going to try a few different things.

    If you have any ideas let me know :)

    You have been a great help!

    Update:
    I have tried turning off my firewall on my PC, i tried opening up the ports on my router to this PC just to see if it would work but its not made any difference. Can't think of anything else that would stop it connecting.
    Last edited by ChrisMayhew; 01-06-2009 at 05:03 PM.

  15. #15
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Just an update, I have managed to get this to work now!

    Thanks so much for all the help.

  16. #16
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    what was the solution?

  17. #17
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by logicbug View Post
    what was the solution?
    I ended up with the Client and the Server in the same project.

    However if I change it from Localhost to the IP address of my computer on my network I can now access it from any PC in my house by just copying the RMIClient.java and RemoteServer.java files over.

    Now comes the hard part of making the chat part..

  18. #18
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

Similar Threads

  1. Java problem
    By grend in forum New To Java
    Replies: 5
    Last Post: 08-18-2008, 11:44 PM
  2. Problem in java
    By saytri in forum New To Java
    Replies: 4
    Last Post: 01-16-2008, 10:09 PM
  3. Problem in java
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-09-2008, 04:13 PM
  4. JAVA if problem
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:58 PM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •