rameshaa
asked on
Dynamic Downloading using RMI
Hi,
I have simple RMI client and server running between 2
machines. I want to download the classes dynamically to client machine.[which are coded in server.]
I couldn't able to download the classes using
java.rmi.server.codebase feature.
Here is the,
RMIServer code
==============
import java.rmi.*;
import java.rmi.server.UnicastRem oteObject;
public
class RemoteServer extends UnicastRemoteObject implements RemoteInterface {
static RemoteServer theServer = null;
String name;
public RemoteServer(String name) throws RemoteException {
super();
this.name = name;
}
public String message(String message) throws RemoteException {
try {
return "My Name is:"+name+",thanks message:"+message;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Foo getObject() {
return new Foo();
}
public static void main (String args[]) {
java.lang.System.out.print ln("Server started.");
java.lang.System.setSecuri tyManager (new RMISecurityManager());
String rmiHostName = "localhost";
String rmiPortNum = "1099";
String serverName = "SimpleServer";
String url = "";
if ( args.length == 2 ) {
rmiHostName = args[0];
rmiPortNum = args[1];
url = "//"+rmiHostName+":"+rmiPo rtNum+"/"+ serverName ;
}
else if ( args.length == 1 ) {
rmiPortNum = args[0];
url = "//"+rmiHostName+":"+rmiPo rtNum+"/"+ serverName ;
}
System.out.println("URL="+ url);
try{
theServer = new RemoteServer (serverName);
Naming.rebind(url,theServe r);
} catch (Exception e){
java.lang.System.out.print ln("An Exception occured while creating server");
e.printStackTrace();
}
}
}
-------------------------- ---------- ---------- ----------
public interface RemoteInterface extends java.rmi.Remote {
String message ( String message ) throws java.rmi.RemoteException;
Foo getObject() throws java.rmi.RemoteException;
}
-------------------------- ---------- ---------- ----------
public class Foo implements java.io.Serializable {
public int myVar;
public Foo() {
}
public Koo getKoo() {
return new Koo();
}
public String toString() {
return "I'm Foo";
}
}
-------------------------- ---------- ---------- ----------
class Koo {
public Koo() { }
public String toString() {
return "I'm Koo";
}
}
-------------------------- ---------- ---------- ----------
import java.rmi.*;
public class RemoteClient {
public static void main(String args[]){
String rmiHostName = "localhost";
String rmiPortNum = "1099";
String serverName = "SimpleServer";
String url = "rmi://"+rmiHostName+":"+r miPortNum+ "/"+server Name;
if ( args.length == 2 ) {
rmiHostName = args[0];
rmiPortNum = args[1];
url = "rmi://"+rmiHostName+":"+r miPortNum+ "/"+server Name;
}
else if ( args.length == 1 ) {
rmiPortNum = args[0];
url = "rmi://"+rmiHostName+":"+r miPortNum+ "/"+server Name;
}
System.out.println("URL="+ url);
java.lang.System.setSecuri tyManager( new RMISecurityManager());
try{
RemoteInterface server = (RemoteInterface)Naming.lo okup(url);
//RemoteInterface server = (RemoteInterface)Naming.lo okup("rmi: //"+args[0 ]+"/Simple Server");
String message = server.message("Hello There");
java.lang.System.out.print ln("The server says :\n" + message);
Foo foo = server.getObject();
Koo koo = foo.getKoo();
} catch (Exception e){
java.lang.System.out.print ln("Error while performing RMI");
e.printStackTrace();
}
}
}
-------------------------- ---------- ---------- ----------
The way I'm running code for server,
in my linux machine, file called run.sh
#!/bin/bash
# NOTE: classpath shouldn't be set for this shell.
rmiregistry &
java -Djava.rmi.server.codebase =http://linuxserver/~user/ -Djava.security.policy=/ho me/user/po licy/my.po licy -classpath ~/public_html/ RemoteServer linuxserver 1099
-------------------------- ---------- ---------- -------
my.policy looks like,
grant {
permission java.net.SocketPermission "*:1024-",
"connect,accept";
permission java.lang.RuntimePermissio n "createSecurityManager";
permission java.lang.RuntimePermissio n "setSecurityManager";
permission java.util.PropertyPermissi on "*", "read, write";
};
-------------------------- ---------- ---------- ---------- -
and I'm running the RemoteClient from Windows2K machine,
java -Djava.security.policy=fil e:/d:/test /dynamicrm i/java1.po licy RemoteClient linuxserver 1099
and the java1.policy looks like,
grant {
permission java.net.SocketPermission "*:1024-",
"connect,accept";
permission java.lang.RuntimePermissio n "createSecurityManager";
permission java.lang.RuntimePermissio n "setSecurityManager";
permission java.util.PropertyPermissi on "*", "read, write";
permission java.net.SocketPermission "linuxserver", "connect,resolve";
};
-------------------------- ---------- ---------- ---------- -
and in the client machine(Windows2K) I copied only
RemoteClient.class & RemoteInterface.class
when I tried to run, I got,
Exception in thread "main" java.lang.NoClassDefFoundE rror: Foo
then I copied Foo, then I got,
Exception in thread "main" java.lang.NoClassDefFoundE rror: Koo
-------------------------- ---------- ---------- ---------- -
Exception in thread "main" java.lang.NoClassDefFoundE rror: Koo
at java.lang.Class.getMethods 0(Native Method)
at java.lang.Class.getDeclare dMethods(C lass.java: 1039)
at java.io.ObjectStreamClass. computeSer ialVersion UID(Object StreamClas s.j
ava:873)
at java.io.ObjectStreamClass. access$200 (ObjectStr eamClass.j ava:46)
at java.io.ObjectStreamClass$ 2.run(Obje ctStreamCl ass.java:4 20)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.io.ObjectStreamClass. init(Objec tStreamCla ss.java:40 1)
at java.io.ObjectStreamClass. lookupInte rnal(Objec tStreamCla ss.java:11 2)
at java.io.ObjectStreamClass. setClass(O bjectStrea mClass.jav a:566)
at java.io.ObjectInputStream. inputClass Descriptor (ObjectInp utStream.j ava
:936)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:366)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:236)
at java.io.ObjectInputStream. inputObjec t(ObjectIn putStream. java:1186)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:386)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:236)
at sun.rmi.server.UnicastRef. unmarshalV alue(Unica stRef.java :300)
at sun.rmi.server.UnicastRef. invoke(Uni castRef.ja va:134)
at RemoteServer_Stub.getObjec t(Unknown Source)
at RemoteClient.main(RemoteCl ient.java: 34)
So the dynamic downloading only downloads "stub" files ??
or Am I doing wrong ?
How do i download the associated classes to clients ???
Is there any sample code explaing how to run ???
or what is the mistake I'm doing ...
Any help will be appreciated.
Thanks alot for reading this big email.
Ramesh//
I have simple RMI client and server running between 2
machines. I want to download the classes dynamically to client machine.[which are coded in server.]
I couldn't able to download the classes using
java.rmi.server.codebase feature.
Here is the,
RMIServer code
==============
import java.rmi.*;
import java.rmi.server.UnicastRem
public
class RemoteServer extends UnicastRemoteObject implements RemoteInterface {
static RemoteServer theServer = null;
String name;
public RemoteServer(String name) throws RemoteException {
super();
this.name = name;
}
public String message(String message) throws RemoteException {
try {
return "My Name is:"+name+",thanks message:"+message;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Foo getObject() {
return new Foo();
}
public static void main (String args[]) {
java.lang.System.out.print
java.lang.System.setSecuri
String rmiHostName = "localhost";
String rmiPortNum = "1099";
String serverName = "SimpleServer";
String url = "";
if ( args.length == 2 ) {
rmiHostName = args[0];
rmiPortNum = args[1];
url = "//"+rmiHostName+":"+rmiPo
}
else if ( args.length == 1 ) {
rmiPortNum = args[0];
url = "//"+rmiHostName+":"+rmiPo
}
System.out.println("URL="+
try{
theServer = new RemoteServer (serverName);
Naming.rebind(url,theServe
} catch (Exception e){
java.lang.System.out.print
e.printStackTrace();
}
}
}
--------------------------
public interface RemoteInterface extends java.rmi.Remote {
String message ( String message ) throws java.rmi.RemoteException;
Foo getObject() throws java.rmi.RemoteException;
}
--------------------------
public class Foo implements java.io.Serializable {
public int myVar;
public Foo() {
}
public Koo getKoo() {
return new Koo();
}
public String toString() {
return "I'm Foo";
}
}
--------------------------
class Koo {
public Koo() { }
public String toString() {
return "I'm Koo";
}
}
--------------------------
import java.rmi.*;
public class RemoteClient {
public static void main(String args[]){
String rmiHostName = "localhost";
String rmiPortNum = "1099";
String serverName = "SimpleServer";
String url = "rmi://"+rmiHostName+":"+r
if ( args.length == 2 ) {
rmiHostName = args[0];
rmiPortNum = args[1];
url = "rmi://"+rmiHostName+":"+r
}
else if ( args.length == 1 ) {
rmiPortNum = args[0];
url = "rmi://"+rmiHostName+":"+r
}
System.out.println("URL="+
java.lang.System.setSecuri
try{
RemoteInterface server = (RemoteInterface)Naming.lo
//RemoteInterface server = (RemoteInterface)Naming.lo
String message = server.message("Hello There");
java.lang.System.out.print
Foo foo = server.getObject();
Koo koo = foo.getKoo();
} catch (Exception e){
java.lang.System.out.print
e.printStackTrace();
}
}
}
--------------------------
The way I'm running code for server,
in my linux machine, file called run.sh
#!/bin/bash
# NOTE: classpath shouldn't be set for this shell.
rmiregistry &
java -Djava.rmi.server.codebase
--------------------------
my.policy looks like,
grant {
permission java.net.SocketPermission "*:1024-",
"connect,accept";
permission java.lang.RuntimePermissio
permission java.lang.RuntimePermissio
permission java.util.PropertyPermissi
};
--------------------------
and I'm running the RemoteClient from Windows2K machine,
java -Djava.security.policy=fil
and the java1.policy looks like,
grant {
permission java.net.SocketPermission "*:1024-",
"connect,accept";
permission java.lang.RuntimePermissio
permission java.lang.RuntimePermissio
permission java.util.PropertyPermissi
permission java.net.SocketPermission "linuxserver", "connect,resolve";
};
--------------------------
and in the client machine(Windows2K) I copied only
RemoteClient.class & RemoteInterface.class
when I tried to run, I got,
Exception in thread "main" java.lang.NoClassDefFoundE
then I copied Foo, then I got,
Exception in thread "main" java.lang.NoClassDefFoundE
--------------------------
Exception in thread "main" java.lang.NoClassDefFoundE
at java.lang.Class.getMethods
at java.lang.Class.getDeclare
at java.io.ObjectStreamClass.
ava:873)
at java.io.ObjectStreamClass.
at java.io.ObjectStreamClass$
at java.security.AccessContro
at java.io.ObjectStreamClass.
at java.io.ObjectStreamClass.
at java.io.ObjectStreamClass.
at java.io.ObjectInputStream.
:936)
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at sun.rmi.server.UnicastRef.
at sun.rmi.server.UnicastRef.
at RemoteServer_Stub.getObjec
at RemoteClient.main(RemoteCl
So the dynamic downloading only downloads "stub" files ??
or Am I doing wrong ?
How do i download the associated classes to clients ???
Is there any sample code explaing how to run ???
or what is the mistake I'm doing ...
Any help will be appreciated.
Thanks alot for reading this big email.
Ramesh//
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.