This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Distributed Java and the fly likes urgent help--SOS-------dynamic class loading in RMI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "urgent help--SOS-------dynamic class loading in RMI" Watch "urgent help--SOS-------dynamic class loading in RMI" New topic
Author

urgent help--SOS-------dynamic class loading in RMI

Jack Deng
Greenhorn

Joined: May 29, 2004
Posts: 23
I can load the _stub.class from file server, but some how it can not load the other return type class file, and I have to copy the return type class file to the client side, otherwise, It woud not run

the code showed as below
============================Customer Return type=============================
package supermarket.server;

public class Customer implements java.io.Serializable
{
private int cust_ID;
private String forename;
private String surname;
private String address;
private String postcode;
private String phone;
private String email;
private String password;

public Customer()
{
//initialising class

cust_ID=0;
forename=null;
surname=null;
address=null;
phone=null;
email=null;
password=null;
}

//methods to set attributes
public void setID(int id)
{
cust_ID=id;
}

public void setForename(String name)
{
forename=name;
}

public void setSurname(String name)
{
surname=name;
}

public void setAddress(String aString)
{
address=aString;
}
public void setPostcode(String aString)
{
postcode=aString;
}
public void setPhone(String aString)
{
phone=aString;
}
public void setEmail(String aString)
{
email=aString;
}
public void setPassword(String aString)
{
password=aString;
}

//accessors to access attributes
public int getID()
{
return cust_ID;
}
public String getForename()
{
return forename;
}
public String getSurname()
{
return surname;
}
public String getAddress()
{
return address;
}
public String getPostcode()
{
return postcode;
}
public String getPhone()
{
return phone;
}
public String getEmail()
{
return email;
}
public String getPassword()
{
return password;
}
}
============================Remote Interface================================
package supermarket.rmi;
import java.rmi.*;
import supermarket.server.Customer;

public interface CustInterface extends Remote
{

public Customer getCustomer(int id) throws RemoteException;
}
=============================Remote object and Server==========================
package supermarket.server;
import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import supermarket.sql.*;
import supermarket.rmi.CustInterface;
import java.sql.*;

public class CustomerServer implements CustInterface{

private Customer aCust;
/**implements CustInterface
*/
public Customer getCustomer(int id) throws RemoteException
{
try
{
DBManipulation db=new DBManipulation(new DatabaseConnection().getConnection());
String sql="select * from customer where cust_ID="+id;
ResultSet r=db.retrieve(sql);
r.next();
aCust=new Customer();
aCust.setForename(r.getString("forename"));

}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
return aCust;
}

public static void main(String[] args)
{
// TODO: Add your code here
try
{

CustomerServer serv=new CustomerServer();
CustInterface remoteObj=(CustInterface)UnicastRemoteObject.exportObject(serv);
Registry r=LocateRegistry.getRegistry("localhost",1099);
r.rebind("Customer",remoteObj);
System.out.println("Server is ready");
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
}
============================Client Side==============================

package supermarket.client;
import java.rmi.*;

import supermarket.rmi.CustInterface;
import supermarket.server.Customer;

public class CustClient
{
public static void main (String[] args)
{
try
{
System.out.println("Connection to server.....");
CustInterface customer=(CustInterface)Naming.lookup("Customer");
Customer aCust=customer.getCustomer(1);
System.out.println(aCust.getForename());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

=============================policy file=================

grant {
permission java.net.SocketPermission "*:1024-", "connect,accept";
Permission java.security.AllPermission;
};

====================Command Line Input==========================
Client files and Server files placed in different partition

==================Server side======================
java -Djava.rmi.server.codebase=http://127.0.0.1:2001/ supermarket.server.CustomerServer

==================client side============================


F:\>java -Djava.security.policy=supermarket/myrmi.policy -Djava.security.manager supermarket.client.CustClient
Connection to server.....
Exception in thread "main" java.lang.NoClassDefFoundError: supermarket/server/Customer
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at supermarket.server.CustomerServer_Stub.<clinit>(Unknown Source)
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
at sun.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
at java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
at java.lang.reflect.Field.getLong(Unknown Source)
at java.io.ObjectStreamClass.getDeclaredSUID(Unknown Source)
at java.io.ObjectStreamClass.access+800(Unknown Source)
at java.io.ObjectStreamClass+3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.<init>(Unknown Source)
at java.io.ObjectStreamClass.lookup(Unknown Source)
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at supermarket.client.CustClient.main(CustClient.java:14)


Jack
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I think the problem is that you reference the Customer class directly in your client code...

Dynamic codebase works well with abstract classes or interfaces. The client has a specification - the abstract class or interface - and the server provides the specific implementation through it's dynamic codebase. The abstract class or interface still has to be exist on the client though.

In your case the Customer class is referenced directly in the client, so this class has to exist on the client side.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
Consider Paul's rocket mass heater.
 
subject: urgent help--SOS-------dynamic class loading in RMI