Hallo,
ich habe ein kleines Problem. Weiß vielleicht jemand was an dem Programm nicht stimmt? Compilieren geht, aber beim Ausführen gibt es die Fehlermeldung: Registring failed, caught exception access denied(java.net.SocketPermission localhost resolve)
Danke angelus
L-ectron-X hat diesen Beitrag am 07.12.2006 um 15:46 Uhr editiert.
Code-Tags eingefügt.
ich habe ein kleines Problem. Weiß vielleicht jemand was an dem Programm nicht stimmt? Compilieren geht, aber beim Ausführen gibt es die Fehlermeldung: Registring failed, caught exception access denied(java.net.SocketPermission localhost resolve)
Code:
import java.rmi.*;
public interface Hello extends Remote {
public String sayHello (String language) throws RemoteException;
}
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl
extends UnicastRemoteObject
implements Hello
{
public HelloImpl() throws RemoteException {
super();
}
public String sayHello(String language) throws RemoteException {
System.out.println ("sayHello: " + language);
String lang = language.toLowerCase();
switch (lang.charAt(0)) {
case 'd': return "Hallo!";
case 'f': return "Salut!";
case 'i': return "Ciao!";
default: return "Hi!";
}
}
}
Code:
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class HelloServer {
public static void main (String[] args /* args[0]: port */) throws RemoteException {
int port = (args.length > 0) ? Integer.parseInt(args[0]) : 4711;
HelloImpl obj = new HelloImpl();
String objName = "HelloObj";
if (System.getSecurityManager() == null) {
System.setSecurityManager (new RMISecurityManager());
}
Registry reg = LocateRegistry.getRegistry (port);
boolean bound = false;
for (int i = 0; ! bound && i < 2; i++) {
try {
reg.rebind (objName, obj);
bound = true;
System.out.println (objName+" bound to registry, port " + port + ".");
}
catch (RemoteException e) {
System.out.println ("Rebinding " + objName + " failed, retrying ...");
reg = LocateRegistry.createRegistry (port);
System.out.println ("Registry started on port " + port + ".");
}
}
System.out.println ("HelloServer ready.");
}
}
Code:
import java.rmi.*;
import java.rmi.server.*;
public class HelloClient {
static public void main (String[] args) /* args[0]: host, args[1]: port*/ {
String host = (args.length < 1) ? "localhost" : args[0];
int port = (args.length < 2) ? 4711 : Integer.parseInt(args[1]);
try {
System.setSecurityManager(new RMISecurityManager()); //<--
Hello obj = (Hello) Naming.lookup("rmi://" + host + ":" + port + "/" + "HelloObj");
System.out.println (obj.sayHello("italiano")); sleep();
System.out.println (obj.sayHello("français")); sleep();
System.out.println (obj.sayHello("Deutsch")); sleep();
System.out.println (obj.sayHello("GB"));
} catch (Exception e) {
System.out.println ("Registring failed, caught exception " + e.getMessage());
}
}
static private void sleep() {
try { Thread.sleep(5000); }
catch (Exception e) { }
}
}
Danke angelus
L-ectron-X hat diesen Beitrag am 07.12.2006 um 15:46 Uhr editiert.
Code-Tags eingefügt.