问题
I am trying to get the Oracle RMI-IIOP example to work, but I'm having problems doing this in Netbeans.
My Code is as follows :
The Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloInterface extends Remote {
public void sayHello(String from) throws RemoteException;
}
The Interface Implementation
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
public class HelloImpl extends PortableRemoteObject implements HelloInterface{
public HelloImpl() throws RemoteException
{
super();
}
public void sayHello(String from) throws RemoteException
{
System.out.println("Hello from " + from + "!!!");
System.out.flush();
}
}
The Server Main
import javax.naming.Context;
import javax.naming.InitialContext;
public class HelloServer {
public static void main(String[] args) {
try {
System.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
System.setProperty("java.naming.provider.url", "iiop://localhost:1050");
// Step 1: Instantiate the Hello servant
HelloImpl helloRef = new HelloImpl();
// Step 2: Publish the reference in the Naming Service
// using JNDI API
Context initialNamingContext = new InitialContext();
initialNamingContext.rebind("HelloService", helloRef);
System.out.println("Hello Server: Ready...");
} catch (Exception e) {
System.out.println("Trouble: " + e);
e.printStackTrace();
}
}
}
And the Client Code
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class HelloRMIIIOPClient {
public static void main(String[] args) {
Context ic;
Object objref;
HelloInterface hi;
try {
ic = new InitialContext();
System.setProperty("classpath", ".");
System.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
System.setProperty("java.naming.provider.url", "iiop://localhost:1050");
// STEP 1: Get the Object reference from the Name Service
// using JNDI call.
objref = ic.lookup("HelloService");
System.out.println("Client: Obtained a ref. to Hello server.");
// STEP 2: Narrow the object reference to the concrete type and
// invoke the method.
hi = (HelloInterface) PortableRemoteObject.narrow(
objref, HelloInterface.class);
hi.sayHello( " MARS " );
} catch( Exception e ) {
System.err.println( "Exception " + e + "Caught" );
e.printStackTrace( );
return;
}
}
}
I've used rmic to generate the stubs and skels and the server part of the code works fine, but when I run the client code I get :
Exception javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initialCaught
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at hellormiiiopnew.HelloRMIIIOPClient.main(HelloRMIIIOPClient.java:33)
BUILD SUCCESSFUL (total time: 0 seconds)
Whilst this is just for me to learn how this works the final code will be used as a template, so I can transfer simple objects on a distributed system over the internet. I've been trying to get RMI to work over the internet with no success so this is my latest attempt. Any help and, especially examples, would be gratefully accepted.
回答1:
You need to set the system properties before creating the InitialContext.
来源:https://stackoverflow.com/questions/30415756/rmi-iiop-over-the-internet