Basic Jini problem and cast exceptions
Jose Hernan Valdes Murguia <[email protected]> Tue, 6 Nov 2007 10:06:24 -0400
| Newsgroups | gmane.comp.java.sun.jini |
|---|---|
| Message-ID | <[email protected]> |
Hello everybody. I have started working with jini always with a single
machine, after some time I decided to distributed my tools using virtual
machines; then, I saw details I have missed before. To warm up, a single
hello program was needed to focus on my future necessities, but I faced two
existential problems(exceptions) that I do not why they appear. Because I do
not know why they appear, I do not also know how to solve them. Then, I
would ask you to help me to understand the roots of my problems, and show my
mistakes. Let's start.
As usual my hello application has three components: the server, the client
and the service(dl package)
hello-server.jar:
net.jini.server.Server.class
hello-dl:
net.jini.core.Hello.class
net.jini.core.HelloServer.class //Hello implementation
hello-client,jar:
net.jini.client.Client.class
net.jini.core.Hello.class
The server works as I expected to do, so I do not have much trouble at all.
However, the client is the problem.
This is the classpath I am using ( I attach this information because I guess
some of them may be the reason of my problem).
JINI_HOME=/opt/java/jini2_1
APP_DIR=./
CLASSPATH=$JINI_HOME/lib/jsk-lib.jar:$JINI_HOME/lib/jsk-platform.jar
CLASSPATH=$CLASSPATH:$APP_DIR/hello-client.jar:$APP_DIR/hello-dl.jar
export CLASSPATH
So, When I execute the following sh script everything works well.
#This works well
java -Djava.rmi.server.codebase=http://sakura:8080/hello-dl.jar \
-Djava.security.policy=$JINI_HOME/common/jsk-all.policy \
net.jini.client.Client 172.16.248.2
Although it works well, I have the hello-dl.jar in my classpath and in my
codebase parameter. as you can see.
However, the implementation will change, so hello-dl.jar will also vary. I
can imagine change the means of communication from sockets to webservices,
it does not matter. According to the Jini promise, the client only needs to
know about the interface(Hello.class) to work, and because I do not want
that my client has the HelloServer implementation I package the jar client
thus:
hello-client.jar:
net.jini.client.Client.class
net.jini.core.Hello.class
Obviously, if I need a Hello reference to compile and run. (fool of my ).
The I run the following script to set out client working.
#sh begin
JINI_HOME=/opt/java/jini2_1
APP_DIR=./
CLASSPATH=$JINI_HOME/lib/jsk-
lib.jar:$JINI_HOME/lib/jsk-platform.jar:$APP_DIR/hello-client.jar
export CLASSPATH
java -Djava.rmi.server.codebase=http://sakura:8080/hello-dl.jar \
-Djava.security.policy=$JINI_HOME/common/jsk-all.policy \
net.jini.client.Client 172.16.248.2
#sh end
Then I have this exception:
java.rmi.UnmarshalException: error unmarshalling return; nested exception
is:
java.lang.ClassNotFoundException: net.jini.core.HelloServer
at com.sun.jini.reggie.RegistrarProxy.lookup(RegistrarProxy.java
:120)
at net.jini.client.Client.init(Client.java:25)
at net.jini.client.Client.main(Client.java:72)
Caused by: java.lang.ClassNotFoundException: net.jini.core.HelloServer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
Now, my question is WHY, Why this happened. I search and read some manual
and jini tutorial but I can not answer my question. Maybe I have not sought
well at all. However, I knew that the client does not need to set a codebase
parameter because the lookup server will download all needed files to
execute the service on client machine. Then, using the same path. I tried
this:
java -Djava.security.policy=$JINI_HOME/common/jsk-all.policy
net.jini.client.Client 172.16.248.2
This seems to be the right way, but I have a casting exception.
public void init(){
...
LookupLocator locator = new
LookupLocator("jini://"+ip+":4160");
ServiceRegistrar registrar = locator.getRegistrar();
ServiceTemplate serviceTemplate = new ServiceTemplate(null,new
Class[]{Hello.class}, null);
Object obj = registrar.lookup(serviceTemplate);
System.out.println(" Is instance ["+
Hello.class.isInstance(obj)+"]
"); // prints "Is instance [false] "
Hello hello = (Hello)obj; // There is the exception
...
}
Exception in thread "main" java.lang.ClassCastException:
net.jini.core.HelloServer
at net.jini.client.Client.init(Client.java:42)
at net.jini.client.Client.main(Client.java:72)
Another detail I could add is that my HelloServer service has two
interfaces[ "java.io.Serializable", "net.jini.core.Hello"], but the "Hello"
interface of HelloService seems not to be the same as "Hello" I'm using in
my HelloClient.java. ( Hello is also java.rmi.Remote )
Class []clazz = obj.getClass().getInterfaces();
System.out.println( "["+clazz[0].equals(Hello.class )+"]" );
//that prints "[false]" although there have the same name, "
net.jini.core.Hello"
I can guess some answers. Both hello in server side and client side are not
the same. But is this really the problem? Is how jini works? If I am doing
something wirdo and not correct please let me know about it. But I think my
requirement is simple. I do only need that my client know the interfaces,
but only the interfaces.
I hope this extend explanation can be enough to show you my doubts. Any
reference to a good jini document or article will be welcome.
Regards,
Jose Hernan Valdes Murguia
--------------------------------------------------------------------------
Getting Started: http://www.jini.org/wiki/Category:Getting_Started
Community Web Site: http://jini.org
jini-users Archive: http://archives.java.sun.com/archives/jini-users.html
Unsubscribing: email "signoff JINI-USERS" to [email protected]