?
Solved

Basic RMI Trouble

Posted on 2002-06-10
22
Medium Priority
?
417 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
ID: 7066852
 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
ID: 7066887
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
ID: 7066900
 Have you tried to run the class from within the d:\JavaProjects\rmitest folder and see what happens?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:StevenWood
ID: 7066915
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
ID: 7066930
 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
ID: 7066949
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
ID: 7066959
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
ID: 7067007
 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
ID: 7067012
 By the way I am using Windows NT as well.
0
 

Author Comment

by:StevenWood
ID: 7067015
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
ID: 7067040
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
 
LVL 35

Expert Comment

by:girionis
ID: 7067116
 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
ID: 7067266
 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
ID: 7067290
 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
ID: 7072617
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
ID: 7072633
 Are you still getting the same error message?
0
 

Author Comment

by:StevenWood
ID: 7072640
yip, the same ClassNotFoundException.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7072646
 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
ID: 7072651
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
ID: 7072656
I dont even get to run my RMIClient because the RMIServer wont start thats where the exception is thrown.
0
 

Author Comment

by:StevenWood
ID: 7072707
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 400 total points
ID: 7072724
 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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 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