PDA

View Full Version : Java



^Gatsu^
5th July 2006, 09:24
C'è su questo forum qualche programmatore Java?

Hador
5th July 2006, 09:25
io conosco le basi, lo fo in uni...

Luceen
5th July 2006, 09:44
io conosco le basi, lo fo in uni...

*

^Gatsu^
5th July 2006, 13:44
Provo comunque a descrivervi il mio problema, magari ci capita più di me.
Sto sviluppando un'applicazione che utilizza la RMI(non entro nei dettagli che tanto non sono rilevanti). Quando nel server eseguo il bind dell'oggetto col comando
Naming.rebind("//"+InetAddress.getByName("mio_indirizz_IP")+"/DATE-SERVER", dateServer);
mi da il seguente errore
Remote Exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: rmi.DateServerInterface
java.lang.ClassNotFoundException: rmi.DateServerInterface
at sun.rmi.server.UnicastServerRef.oldDispatch(Unknow n Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages( Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceiv edFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(Str eamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:3 43)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at rmi.DateServer.main(DateServer.java:31)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: rmi.DateServerInterface
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknow n Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(Unknow n Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages( Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: rmi.DateServerInterface
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 java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(U nknown Source)
at sun.rmi.server.LoaderHandler.loadProxyClass(Unknow n Source)
at sun.rmi.server.LoaderHandler.loadProxyClass(Unknow n Source)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(Un known Source)
at java.rmi.server.RMIClassLoader.loadProxyClass(Unkn own Source)
at sun.rmi.server.MarshalInputStream.resolveProxyClas s(Unknown Source)
at java.io.ObjectInputStream.readProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unkno wn Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
... 9 more

Ho inserito in grassetto l'errore vero e proprio, il resto è il risultato del printStackTrace() che dovrebbe servire per il debugging.
Idee? :D

Alkabar
5th July 2006, 13:53
Intanto:



Marshalling:
The process of gathering data and transforming it into a standard format before it is transmitted over a network so that the data can transcend network boundaries. In order for an object to be moved around a network, it must be converted into a data stream that corresponds with the packet structure of the network transfer protocol. This conversion is known as data marshalling. Data pieces are collected in a message buffer before they are marshaled. When the data is transmitted, the receiving computer converts the marshaled data back into an object.
Data marshalling is required when passing the output parameters of a program written in one language as input to a program written in another language.


Fammi vedere il codice del server e del client java per le RMI. E soprattutto dimmi che deve fare.

Alkabar
5th July 2006, 13:59
http://www.ccs.neu.edu/home/kenb/com3337/rmi_tut.html

Qui ci sono degli esempi.

Quale editor usi intanto ?

^Gatsu^
5th July 2006, 16:18
Allora, io lavoro sotto Windows ed uso Netbeans.
Client

package rmi;
import java.rmi.RemoteException;
import java.rmi.Naming;
public class DateClient {
public static void main(String[] args) {
System.setSecurityManager(new SecurityManager());
try{
String url = "//localhost/DATE-SERVER";
DateServerInterface remoteObject = (DateServerInterface) Naming.lookup(url);
System.out.println("Date: " + remoteObject.getDate());
}catch(RemoteException re){
System.out.println(re);
}catch(java.net.MalformedURLException ex){
System.out.println("Malformed url: " + ex);
}catch(java.rmi.NotBoundException ne){
System.out.println(ne);
}
}
}
Server

package rmi;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class DateServer{
public static void main(String[] args){
try{
System.setSecurityManager(new RMISecurityManager());
System.out.println("Security Manger settato");
DateServerImpl dateServer = new DateServerImpl();
System.out.println("Creata una nuova istanza di DateServerImpl");
Naming.rebind("//"+InetAddress.getByName("xxx.xxx.xxx.xxx")+"/DATE-SERVER", dateServer);
System.out.println("Date Server created......");
}catch(java.net.MalformedURLException me){
System.out.println("Malformed URL: " + me.toString());
me.printStackTrace();
}catch(RemoteException re){
System.out.println("Remote Exception: " + re.toString());
re.printStackTrace();
}catch(UnknownHostException e){
System.out.println(e);
}
}
}
Premetto che il codice è corretto. L'applicazione recupera la data dal server e la stampa a schermo.
Di esempi ne ho parecchi fra le mani, non ho fatto che leggere tutorial e documentazione; il problema è che non riesco a far girare i programmi.

Alkabar
5th July 2006, 16:29
Ok, così su due piedi mi verrebbe da dire:

java.lang.ClassNotFoundException: rmi.DateServerInterface


non trova questa interfaccia, di conseguenza non riesce a fare il marshalling, di conseguenza ti lancia una eccezione. Sicuro di avere incluso tutto quello che va incluso ?

^Gatsu^
5th July 2006, 16:50
Le classi si trovano tutte nella stessa cartella ed appartengono allo stesso package.
Non so quindi cosa possa non aver incluso.

Alkabar
5th July 2006, 17:14
Dammi anche le interfacce e gli oggetti che le implementano che ti sei fatto tu per l'esercizio.

Alkabar
5th July 2006, 18:34
Intanto che attendo... prova così:

cambia questa riga

DateServerImpl dateServer = new DateServerImpl();

con

DateServerInterface dateServer = new DateServerImpl();

^Gatsu^
6th July 2006, 07:49
Ho provato ad apportare il cambiamento, ma l'errore persiste. Comunque queste sono le interfacce.

package rmi;

import java.rmi.*;
import java.rmi.server.*;
import java.util.Date;

import java.rmi.server.UnicastRemoteObject;

public class DateServerImpl extends UnicastRemoteObject implements DateServerInterface{

public DateServerImpl() throws RemoteException{
super();
}

public String getDate() throws RemoteException{
return new Date().toString();
}
}

package rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Date;

public interface DateServerInterface extends Remote{
String getDate() throws RemoteException;
}

Comunque da quello che ho trovato in rete potrebbe essere un problema del classpath del registry. Purtroppo non ho molta confidenza con la RMI, mi trovo molto meglio con socket.

^Gatsu^
6th July 2006, 08:02
Allora, dopo una mare di ricerche sono riuscito a risolvere il problema.
Praticamente non era settato a dovere il classpath del registry. Le soluzioni sono due:
1)lo setti
2)fai una copia del registry nella cartella di lavoro

Ho optato per la due dato che su winzozz non so come si settano le variabili d'ambiente; non ci sta il setenv. :(

Darath
6th July 2006, 08:04
sto errore me lo dava pure a me (simile almeno, non ho letto tutta la stacktrace)
3 cose:
1 fai partire il registry di RMI (sta dnetro la directory bin della jdk e si chiama rmi registry)?
2 dopo aver compilato le classi, compili in rmi la classe DataServerImpl con il comando "rmic" da prompt del dos? E subito dopo registri in java la classe col main ? (comando java NomeClasseServer sempre dal prompt del dos)
3 dopo aver fatto questo ti sei ricordato di copiare il conseguente STUB che viene creato nella directory del client?

io mi scordavo di copiare lo stub :P e l'errore era molto simile :)

in bocca al lupo

p.s. ricordati di impostare il classpath della jdk (gestione risorse, proprieta, avanzate, variabili d'ambiente) cosi puoi usare il comando rmic e il comando java dalla directory di lavoro

Darath
6th July 2006, 08:05
ecco hai risolto mentre scrivevo :P
meglio cosi' :D

cmq per settare il classpath ti ho scritto nel post subito sopra come si fa :)

^Gatsu^
6th July 2006, 08:34
Infatti ora i problemi sono proprio con lo stub. :D

Grazie mille ad entrambi per l'aiuto, siete stati gentilissimi.

Alkabar
6th July 2006, 08:57
Infatti ora i problemi sono proprio con lo stub. :D

Grazie mille ad entrambi per l'aiuto, siete stati gentilissimi.


Visto che era un problema di classpath :D.

^Gatsu^
6th July 2006, 09:50
Lavorare con la RMI è un vero parto. Ora il client non si connette al server e mi da Connection timed out.:madd:

Ho impiegato una settimana per sviluppare il 90% del progetto, usando TCP e UDP, e sono sei giorni bloccato dalla RMI. :gha:

Gate
6th July 2006, 12:38
le variabili d'ambiente le trovi sulle proprieta' del sistema
sotto avanzate

Alkabar
6th July 2006, 14:58
Lavorare con la RMI è un vero parto. Ora il client non si connette al server e mi da Connection timed out.:madd:
Ho impiegato una settimana per sviluppare il 90% del progetto, usando TCP e UDP, e sono sei giorni bloccato dalla RMI. :gha:


Se non si connette stai sbagliando indirizzo ip o numero di porta. Per provare, dove scrivi inet.get qualcosa, toglilo e metti localhost .

RMI a me non piacciono tanto, in tutta sincerità le ho usate solo una volta un paio di anni fa in un progettino.

però senza o vai ad alto livello con agenti e infrastrutture ad agenti... o vai a basso livello con socket e quant'altro ... anche se in tutta sincerità l'interfaccia socket di java ... più o meno mi fa schifo quanto rmi ...