Skip to main content
gamedev.net
🔒 Locked

[java] RMI

Started by frankfrank 3 replies 1.5k views
Original Post
frankfrank
frankfrank
I'm trying to write an hello world rmi application, but I keep getting an error java.lang.ClassNotFoundException: javainterface Why do i get this error? and how do I solve it? I have 3 classes: main.java

import java.*;
import java.math.BigInteger;
import java.rmi.*;

public class main 
{

	  public static void main (String[] argv) 
	  {
	
		  
     	// Create and install a security manager
		 if(System.getSecurityManager() == null) {
		  System.setSecurityManager(new RMISecurityManager());
		 }
		
		  try {
	      Naming.rebind ("rmi://127.0.0.1:1099/", new hello ("Hello, world!"));
	      System.out.println ("Hello Server is ready.");
	    } catch (Exception e) {
	      System.out.println ("Hello Server failed: " + e);
	    }
	  }
}

javainterface.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface javainterface extends Remote 
{

	public String say() throws RemoteException;
}

and hello.java

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

public class hello extends UnicastRemoteObject implements javainterface 
{
  private String message;

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

  public String say() throws RemoteException {
    return message;
  }
}
I have rmiregistry running and Im using eclipse ide. [Edited by - frankfrank on May 28, 2009 6:00:09 AM]
Lord_Evil
Lord_Evil
javaiterface doesn't seem to be in the class path.

Is javainterface compiled?
Where is the .class file located?

Other note: You should definitely work on your naming. There are several conventions that make your life easier:
- place your classes in packages
- class names are captialized camel case, e.g. Main (so you don't mix it up with the main method)
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
frankfrank
frankfrank
Right now the .class files are in the project direcotry
C:\Documents and Settings\Win\workspace2\rmiserver

How can I set the classpath in eclipse? isnt it done automatically?
frankfrank
frankfrank
i got it working. both client and server have to be in the same package.
I can start it now within netbeans or eclipse, but how can I start it from a console?

I tried: java client
but I get the following:

Exception in thread "main" java.lang.NoClassDefFoundError: clientCaused by: java.lang.ClassNotFoundException: client        at java.net.URLClassLoader$1.run(Unknown Source)        at java.security.AccessController.doPrivileged(Native Method)        at java.net.URLClassLoader.findClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)        at java.lang.ClassLoader.loadClass(Unknown Source)        at java.lang.ClassLoader.loadClassInternal(Unknown Source)Could not find the main class: client.  Program will exit.


cbotman
cbotman
You need to set the java.rmi.server.codebase property for this to run standalone.

The RMI Registry uses the codebase to locate the class files of any objects that will be available remotely (so that clients can download the classes).

If the client is going to pass objects as parameters to your server, you may need to set the codebase for your client too--but in your example, that won't be needed.

See the following (might need to search for 'codebase'):
http://java.sun.com/j2se/1.5.0/docs/guide/rmi/codebase.html
http://java.sun.com/docs/books/tutorial/rmi/running.html
http://java.sun.com/j2se/1.4.2/docs/guide/rmi/javarmiproperties.html

You can set the codebase in code or via the command line, like so:

java -Djava.rmi.server.codebase="file:/%CD%/server.jar" -jar server.jar

That's for Windows. Replace %CD% with the directory 'server.jar' is sitting in. And replace 'server.jar' with whatever you compile your server as.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.