Solved

Basic RMI Trouble

Posted on 2002-06-10
22
407 Views
Last Modified: 2013-12-29
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.
0
Comment
Question by:StevenWood
  • 11
  • 11
22 Comments
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 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
 

Author Comment

by:StevenWood
Comment Utility
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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 Have you tried to run the class from within the d:\JavaProjects\rmitest folder and see what happens?
0
 

Author Comment

by:StevenWood
Comment Utility
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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 Sorry, I have run out of ideas. Can you post your code here so I can compile it and try it as well?

0
 

Author Comment

by:StevenWood
Comment Utility
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
 

Author Comment

by:StevenWood
Comment Utility
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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 By the way I am using Windows NT as well.
0
 

Author Comment

by:StevenWood
Comment Utility
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
 

Author Comment

by:StevenWood
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 35

Expert Comment

by:girionis
Comment Utility
 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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 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
 

Author Comment

by:StevenWood
Comment Utility
argh ! I still can't get it to work.  Any other ideas ?  You can get the points anyway, thanks for your help.

Steve.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 Are you still getting the same error message?
0
 

Author Comment

by:StevenWood
Comment Utility
yip, the same ClassNotFoundException.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 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
 

Author Comment

by:StevenWood
Comment Utility
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
 

Author Comment

by:StevenWood
Comment Utility
I dont even get to run my RMIClient because the RMIServer wont start thats where the exception is thrown.
0
 

Author Comment

by:StevenWood
Comment Utility
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
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
Comment Utility
 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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now