gpt4 book ai didi

java - 通过互联网的 RMI iiop

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:55 24 4
gpt4 key购买 nike

我正在尝试获取 Oracle RMI-IIOP example工作,但我在 Netbeans 中执行此操作时遇到问题。

我的代码如下:

界面

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloInterface extends Remote {
public void sayHello(String from) throws RemoteException;
}

接口(interface)实现

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();
}
}

服务器主

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();
}
}
}

和客户端代码

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;
}
}
}

我使用 rmic 生成 stub 和骨架,代码的服务器部分工作正常,但是当我运行客户端代码时,我得到:

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)

虽然这只是为了让我了解其工作原理,但最终代码将用作模板,因此我可以通过互联网在分布式系统上传输简单的对象。我一直在尝试让 RMI 在互联网上工作,但没有成功,所以这是我的最新尝试。任何帮助,尤其是示例,将不胜感激。

最佳答案

您需要在创建InitialContext 之前设置系统属性。

关于java - 通过互联网的 RMI iiop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com