[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1949
  • Last Modified:

Need help getting this simple Java RMI program running

I've been pulling my hair out trying to get RMI running on windows.  I decided to do a verbatim copy of a tutorial to see if it would work, well it didn't.  Here is the sample code:

//HelloInterface.java - located in dir c:\java
//
import java.rmi.*;

public interface HelloInterface extends Remote {

  public String say() throws RemoteException;
}

//Hello.java - located in c:\java
//
import java.rmi.*;
import java.rmi.server.*;

public class Hello extends UnicastRemoteObject implements HelloInterface {
  private String message;

  public Hello (String msg) throws RemoteException {
    message = msg;
  }

  public String say() throws RemoteException {
    return message;
  }
}


//HelloServer.java - located in c:\java
import java.rmi.*;
import java.rmi.Naming;

public class HelloServer{

  public static void main (String[] argv) {
    try {
      Naming.rebind ("Hello", new Hello ("Hello, world!"));
      System.out.println ("Hello Server is ready.");
    } catch (Exception e) {
      System.out.println ("Hello Server failed: " + e);
    }
  }

}



Then I open a command prompt and go to c:\java and type:
C:\java>javac HelloInterface.java
C:\java>javac Hello.java
C:\java>rmic Hello
C:\java>javac HelloServer.java

I then open a second command prompt and navigate to c:\registry to (to avoid being in the same directory as the stub files) and type:
C:\register>C:\j2sdk1.4.2_03\bin\rmiregistry

In the original command prompt, I then run HelloServer.  I've tried running it a 100 different ways and always get the same error:
Hello Server failed: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
        java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
        java.lang.ClassNotFoundException: Hello_Stub

I've tried just running it as:
java HelloServer
java -classpath . HelloServer

C:\java>java -Djava.rmi.server.codebase=http://localhost -classpath . HelloServer

C:\java>java -Djava.security.policy=..\policy.all -Djava.rmi.server.codebase=http://localhost -classpath . HelloServer

Where policy.all is:
grant {
      permission java.security.AllPermission;
};


And pretty much just about every combination of the above that you can think of.

0
JDmears2
Asked:
JDmears2
1 Solution
 
b3lzCommented:
       java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
        java.lang.ClassNotFoundException: Hello_Stub

^^ there is something wrong with your stub/skeleton, be sure to first create them before trying to use them. If you don't first make the stubs and skeletons, you will get that ClassNotFoundException. Most IDE's will make them for you.

The nested marshalling exception is because the stubs/skeleton are needed to translate your objects etc to something that can be transferred over the wire. You created the hello with rmic, but be sure to make them both of server and client.

More info on rmic you can find here:

http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/rmic.html

Good luck,

greetings, belz.
0
 
kiranhkCommented:
your code should be

public class HelloServer{

  public static void main (String[] argv) {
    try {
      Naming.rebind ("//localhost/HelloServer", new Hello ("Hello, world!"));
      System.out.println ("Hello Server is ready.");
    } catch (Exception e) {
      System.out.println ("Hello Server failed: " + e);
    }
  }

}

http://java.sun.com/j2se/1.3/docs/guide/rmi/getstart.doc.html#5912

http://java.sun.com/j2se/1.3/docs/guide/rmi/getstart.doc.html
0
 
JDmears2Author Commented:
b3lz:  Well, I'm not trying to get a client working yet, because it wouldn't do much good if the server isn't working.    The stub and skeleton files are in the same directory as HelloServer.java

kiranhk:  I tried changing the first argumnet to Naming.rebind as you suggested, but I received the same error message.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
JDmears2Author Commented:
I also just tried adding:

            if (System.getSecurityManager() == null) {
                System.setSecurityManager(new RMISecurityManager());
            }

As the first lines to the main method in HelloServer and if I try to run it after recompiling without the policy file, I get an AccessControlException.  If I then try to also include the policy file, I get the same error as before.
0
 
kiranhkCommented:
ok, assuming that you are in the same folder as your generated Stubs

then try this

rmic -classpath . Hello
0
 
objectsCommented:
try:

java -Djava.rmi.server.codebase=file:///java/ HelloServer
0
 
JDmears2Author Commented:
What objects posted was almost the ticket.  I still needed to add the security policy though.  Any reason why I need these two switches when all the examples online are able to just type java HelloServer and be on their marry way?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now