下面四个文件分别是接口 server,client,policy
import java.rmi.*;
public interface Hello extends Remote{
String sayHello()throws RemoteException;
}
import javax.naming.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import javax.rmi.PortableRemoteObject;
public class HelloImpl extends PortableRemoteObject implements Hello{
public HelloImpl()throws RemoteException{
super();
}
public String sayHello(){
return "Hello World";
}
public static void main(String arp[]){
if(System.getSecurityManager()==null){
System.setSecurityManager(new RMISecurityManager());
}
try{
Hello obj=new HelloImpl();
Context initialNamingContext=new InitialContext();
initialNamingContext.rebind("helloServer", obj);
System.out.println("HelloServer bound in registry");
}catch(Exception e){
System.out.println("HelloImpl err:"+e.getMessage());
e.printStackTrace();
}
}
}
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
public class HelloClient {
public static void main(String arp[])throws Exception{
System.setSecurityManager(new RMISecurityManager());
Context initialNamingContext=new InitialContext();
Hello RemoteObj=(Hello)PortableRemoteObject.narrow(initialNamingContext.lookup("iiop://localhost:7001/helloServer"), Hello.class);
System.out .println(RemoteObj.sayHello());
}
}
grant{
//Allow everything for now
permission java.security.AllPermission;
};
下面启动服务器顺利
F:\JAVA APPLICATION\网络\11\RMI-IIOP JNDI\src\jdeveloper\rmi_iiop>java -Djava.na
ming.factory.initial=weblogic.jndi.WLInitialContextFactory -Djava.naming.provide
r.url=iiop://localhost:7001 -Djava.security.policy=policy HelloImpl
HelloServer bound in registry
但是启动client的时候出错
F:\JAVA APPLICATION\网络\11\RMI-IIOP JNDI\src\jdeveloper\rmi_iiop>java -Djava.na
ming.factory.initial=weblogic.jndi.WLInitialContextFactory -Djava.provider.url=i
iop://localhost:7001 -Djava.security.policy=policy HelloClient
Exception in thread "main" java.rmi.ConnectException: Connection refused: connect; nested exception is:
        java.net.ConnectException: Connection refused: connect
        at weblogic.iiop.Utils.mapToRemoteException(Utils.java:739)
        at weblogic.iiop.IIOPRemoteRef.invoke(IIOPRemoteRef.java:171)
        at Hello_IIOP_WLStub.sayHello(Unknown Source)
        at HelloClient.main(HelloClient.java:9)
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Unknown Source)
        at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at weblogic.socket.SocketMuxer.newSocket(SocketMuxer.java:311)
        at weblogic.socket.ChannelSocketFactory.createSocket(ChannelSocketFactory.java:82)
        at weblogic.socket.AbstractMuxableSocket.createSocket(AbstractMuxableSocket.java:209)
        at weblogic.socket.AbstractMuxableSocket.createSocket(AbstractMuxableSocket.java:200)
        at weblogic.iiop.MuxableSocketIIOP.newSocket(MuxableSocketIIOP.java:262)
        at weblogic.iiop.MuxableSocketIIOP.createSocket(MuxableSocketIIOP.java:249)
        at weblogic.socket.AbstractMuxableSocket.connect(AbstractMuxableSocket.java:181)
        at weblogic.iiop.MuxableSocketIIOP.connect(MuxableSocketIIOP.java:234)
        at weblogic.iiop.MuxableSocketIIOP.createConnection(MuxableSocketIIOP.java:218)
        at weblogic.iiop.EndPointManager.createEndPoint(EndPointManager.java:450)
        at weblogic.iiop.EndPointManager.findOrCreateEndPoint(EndPointManager.java:237)
        at weblogic.iiop.EndPointManager.findOrCreateEndPoint(EndPointManager.java:254)
        at weblogic.iiop.IIOPRemoteRef.locateIORForRequest(IIOPRemoteRef.java:383)
        at weblogic.iiop.IIOPRemoteRef.locateRequest(IIOPRemoteRef.java:377)
        at weblogic.iiop.IIOPRemoteRef.invoke(IIOPRemoteRef.java:156)
        ... 2 more

解决方案 »

  1.   

    Caused by: java.net.ConnectException: Connection refused: connect java.net.ConnectException: Connection refused: connect 拒绝连接出现这个问题有可能是多个原因

    最有可能是的是防火墙的问题,你关上防火墙试试
    要是不想关就把你的端口添加到防火墙的例外中就可以了
    要是不是上面的问题
    你的数据库使用的是什么?你看一下服务器的TCP/IP是否被禁用了,sqlserver2005默认情况下是禁用了TCP/IP连接。启动TCP/IP连接的方法:开始\程序\Microsoft SQL Server 2005\配置工具\目录下的SQL Server Configuration Manager,选择mssqlserver协议, 然后右边窗口有个tcp/ip协议,然后启动它,把sqlserver服务停了再启动。 或是 
    设置 “开始\程序\Microsoft SQL Server 2005\配置工具\SQL Server 外围应用配置器” 在“SQL Server 2005 外围应用配置器”页, 单击“服务和连接的外围应用配置器” ,然后单击展开“数据库引擎”, 选中“远程连接”,在右边选中“本地连接和远程连接”, 再选择要使用的协议,( 这个地方应当启用TCP/IP 和命名管道服务!)单击“应用”,您会看到下消息: 
    “直到重新启动数据库引擎服务后,对连接设置所做的更改才会生效。”单击“确定”按钮返回 。 或是防火墙的问题:你把1433的端口加到例外去就可以了。 
    基本上就是上面三个问题。如果不可以的话,你再查一下资料吧,网上很多。
      

  2.   

    楼主看看:
    http://java.sun.com/j2se/1.4.2/docs/guide/rmi/getstart.doc.html
      

  3.   

    还有这篇文章也很好:
    http://java.sun.com/j2se/1.4.2/docs/guide/rmi-iiop/tutorial.html
      

  4.   

    啊  崩溃了  pathuang68大牛推荐的网页上的实现了  但是使用的是jdk自带的ordb服务吧,我这个用的是weblogic的就是不能实现
      

  5.   

    System.setSecurityManager(new RMISecurityManager());
    楼主试着把这句删掉,我好像遇到过,不一定行。
      

  6.   

    删掉了  还是不行,我把HelloClient.java加了
    System.out.println(RemoteObj.toString());
    都能正确输出Delegate(3201069) [weblogic.iiop.IOR[RMI:Hello:0000000000000000] @192.168.1.4:2062, <257, null>]
    怎么下一句System.out .println(RemoteObj.sayHello());就不能执行了呢?