Basic RMI Trouble

Hi all,

I have just started using RMI and am having a problem as follows :-

I have created my classes in a package named rmitest.  The server class is a simple class called HelloWorld and I have succesfully created and compiled the HelloWorldImpl class.  The class HelloWorldImpl_Stub has been created and is in the same directory as HelloWorldImpl but when I try and start the main class that calls Naming.rebind(), i get a no class def found error.  

I have a directory called d:\JavaProjects\rmitest  the files in here are in a package called rmitest.  The files created by rmic are also in this directory. I invoked

rmic -d d:\JavaProjects -classpath d:\JavaProjects rmitest.HelloWorldImpl

This creates the HelloWorldImpl_Stub class and the HelloWorldImpl_Skel.class an puts them in d:\JavaProjects\rmitest

Then I start the registry tool.

Now when I try to invoke the main class (I called it RMIServer) by using
java -classpath d:\JavaProjects rmitest.RMIServer

I get a NoClassDefFound error, complaining it can't find rmitest.HelloWorldImpl_Stub.  When I change the name of the main class parameter(to rmitest.HelloWorldImpl_Stub) to the VM, it finds it OK but complains of no main method as I would expect.  So that proves the class is there and in the right package.


Any ideas ?

Cheers
Steve.
StevenWoodAsked:
Who is Participating?
 
girionisConnect With a Mentor Commented:
 I see.... I was invoking it from the folder where my java source files are as well (c:\java) while my classes are in the c:\java\rmitest. This is whay I had -classpath . and it was looking in the current folder.

  Finally :-)
0
 
girionisCommented:
 Well, everything seems fine from your description.


  Have you tried to be in the d:\JavaProjects\rmitest folder and do a:

java -classpath . RMIServer

  and see what happens? Could it also be that there is one mor folder within the rmitest (so you get a tree like: d:\JavaProject\rmitest\rmitest)?

  Hope it helps.
0
 
StevenWoodAuthor Commented:
No, there is no extra folder within rmitest.  Also I tried using JBuilder to show the methods and package declaration for a .class file and it shows that both the classes created by rmic are in the package rmitest.

Thanks anyway
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
girionisCommented:
 Have you tried to run the class from within the d:\JavaProjects\rmitest folder and see what happens?
0
 
StevenWoodAuthor Commented:
yep, i tried that too.  it doesn't work at all because the class name is rmitest.RMIServer so if I try

d:\javaprojects\rmitest> java RMIServer

this is noclassdeffound because the class is called rmitest.RMIServer

and

d:\JavaProjects\rmitest> java rmitest.RMIServer

fails also because VM looks for a folder called rmitest inside rmitest i.e. d:\JavaProjects\rmitest\rmitest
0
 
girionisCommented:
 Sorry, I have run out of ideas. Can you post your code here so I can compile it and try it as well?

0
 
StevenWoodAuthor Commented:
yep, i tried that too.  it doesn't work at all because the class name is rmitest.RMIServer so if I try

d:\javaprojects\rmitest> java RMIServer

this is noclassdeffound because the class is called rmitest.RMIServer

and

d:\JavaProjects\rmitest> java rmitest.RMIServer

fails also because VM looks for a folder called rmitest inside rmitest i.e. d:\JavaProjects\rmitest\rmitest
0
 
StevenWoodAuthor Commented:
OK, are you running on windows ?  I'm using windows NT 4.

Here are the files (as you can see they are very simple :-
i use ------- to separate each file.

------------------------------------------------------

package rmitest;

import java.rmi.*;

public interface HelloWorld extends Remote
{

  public String getMessage() throws RemoteException;

}

----------------------------------------------------------


package rmitest;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.Naming;

public class HelloWorldImpl extends UnicastRemoteObject implements HelloWorld
{

  public HelloWorldImpl()
  throws RemoteException
  {
  }

  public String getMessage()
  throws RemoteException
  {
    return "Hello World.";
  }

}


--------------------------------------------------------

package rmitest;

import java.rmi.Naming;


public class RMIServer
{

  public static void main(String[] args)
  throws Exception
  {
    HelloWorldImpl hello = new HelloWorldImpl();
    Naming.rebind("hello", hello);
  }

}

----------------------------------------------------------

package rmitest;

import java.rmi.Naming;

public class RMIClient
{

  public static void main(String[] args)
  throws Exception
  {

    HelloWorld hello = (HelloWorld) Naming.lookup("rmi://localhost//hello");
    System.out.println(hello.getMessage());

  }

}

--------------------------------------------------------



The order i did things was
1) Compile classes using javac
2) compile rmitest.HelloWorldImpl using rmic
3) start rmiregistry
4) try and run the program RMIServer

Thanks
Steve.
0
 
girionisCommented:
 Well it works on my machine... I am getting of course the ConnectException but this is because I am not running the rmiregistry. The steps I followed:

  1) created a folder called java (c:\java)
  2) Copied all your files in there (c:\java\HelloWorldImpl.java etc)
  3) Compiled everything using: javac -d . *.java
  This created a folder called rmitest within the java folder that contans all the class files (c:\java\rmitest\HelloWorldImpl.class etc).
  4) Used rmic to create stubs and skeletons: rmic -d . -classpath . rmitest.HelloWorldImpl
  5) Ran it using: java -classpath . rmitest.RMIServer

  Hope it helps. Maybe you should have used the "." (current directory) instead of setting it usind d:\JavaProjects

  Hope it helps.
0
 
girionisCommented:
 By the way I am using Windows NT as well.
0
 
StevenWoodAuthor Commented:
OK, are you running on windows ?  I'm using windows NT 4.

Here are the files (as you can see they are very simple :-
i use ------- to separate each file.

------------------------------------------------------

package rmitest;

import java.rmi.*;

public interface HelloWorld extends Remote
{

  public String getMessage() throws RemoteException;

}

----------------------------------------------------------


package rmitest;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.Naming;

public class HelloWorldImpl extends UnicastRemoteObject implements HelloWorld
{

  public HelloWorldImpl()
  throws RemoteException
  {
  }

  public String getMessage()
  throws RemoteException
  {
    return "Hello World.";
  }

}


--------------------------------------------------------

package rmitest;

import java.rmi.Naming;


public class RMIServer
{

  public static void main(String[] args)
  throws Exception
  {
    HelloWorldImpl hello = new HelloWorldImpl();
    Naming.rebind("hello", hello);
  }

}

----------------------------------------------------------

package rmitest;

import java.rmi.Naming;

public class RMIClient
{

  public static void main(String[] args)
  throws Exception
  {

    HelloWorld hello = (HelloWorld) Naming.lookup("rmi://localhost//hello");
    System.out.println(hello.getMessage());

  }

}

--------------------------------------------------------



The order i did things was
1) Compile classes using javac
2) compile rmitest.HelloWorldImpl using rmic
3) start rmiregistry
4) try and run the program RMIServer

Thanks
Steve.
0
 
StevenWoodAuthor Commented:
OK this helps, its still not working but when I dont start the rmiregistry, i get the same error as you.  ConnectionRefused

When I start the rmiregistry, i get the NoClassDefFoundError still.  Why can't you start the rmiregistry tool ?
0
 
girionisCommented:
 Then you probably need to set the system property to download the stub classes dynamicaly to the registry and then you can try running the client again...
  http://java.sun.com/products/jdk/1.1/docs/guide/rmi/getstart.doc.html#5522

  I will try it as well and let you know what happened.
0
 
girionisCommented:
 Ok I have tried it properly. It works fine...

  start the rmiregistry with: start rmiregistry and then run the server using: java -classpath . -Djava.rmi.server.codebase=http://locahost/java/ rmitest.RMIServer (you will need to substitute "java" with your "JavaProject")

  Then run the client with java -classpath . rmitest.RMIClient and you will get the messages:

 Starting hello...
 Hello World.

  By the way you will need to chaneg the: Naming.lookup("rmi://localhost//hello"); (in your client file) to Naming.lookup("rmi://localhost/hello");

  Hope it helps.
0
 
girionisCommented:
 Well I have just tried it without setting the rmi codebase system property and works as well.

  1) start rmiregistry
  2) java -classpath . rmitest.RMIServer
  3) java -classpath . rmitest.RMIClient

  Hope it helps.
0
 
StevenWoodAuthor Commented:
argh ! I still can't get it to work.  Any other ideas ?  You can get the points anyway, thanks for your help.

Steve.
0
 
girionisCommented:
 Are you still getting the same error message?
0
 
StevenWoodAuthor Commented:
yip, the same ClassNotFoundException.
0
 
girionisCommented:
 Weird... I have just tried it again and it works fine... I can see the "Hello World" message on the screen when I start the client... have you taken out the second "/" from the "rmi://localhost//hello" string so it looks like: "rmi://localhost/hello"?
0
 
StevenWoodAuthor Commented:
Here is the actual error trace :-    

   java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
        java.lang.ClassNotFoundException: rmitest.HelloWorldImpl_Stub
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknow
n Source)
        at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown 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 rmitest.RMIServer.main(RMIServer.java:14)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
ception is:
        java.lang.ClassNotFoundException: rmitest.HelloWorldImpl_Stub
        ... 6 more
Caused by: java.lang.ClassNotFoundException: rmitest.HelloWorldImpl_Stub
        ... 6 more
0
 
StevenWoodAuthor Commented:
I dont even get to run my RMIClient because the RMIServer wont start thats where the exception is thrown.
0
 
StevenWoodAuthor Commented:
IT WORKS!!!!!

I got it to work by invoking rmiregistry from d:\JavaProjects rmiregistry must look there for classes.  I looked at the doc for rmiregistry and i dont know how you specify a classpath.  I was invoking it from jdk1.4\bin

ah well....

Thanks anyway
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.