Zeroc ice java eclipse slice2java

本文介绍了如何在Java环境下使用Zeroc ICE构建一个简单的Hello World应用程序。首先,下载并安装ICE及其Eclipse插件,然后创建一个名为`hello.ice`的Slice文件定义接口。接着,利用ICE Builder自动生成Java代码,并实现接口。最后,分别构建并启动服务端和客户端,完成通信。示例代码及控制台输出展示了完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 下载  ice

https://download.zeroc.com/Ice/3.6/Ice-3.6.2.msi

我的安装地址为


eclipse 安装 ICE  builder for eclipse

安装完成后配置


到此环境准备好了

下面建工程 ,写代码了

2. 建ice_hello  project

在project下建目录 slice

然后建 hello.ice 文件

文件名大小写无所谓

文件内容

 [["java:package:com.stone.hello"]] // 定义java包名  
    module testice  
    {  
        interface Hello  
        {  
            string sayHello(string s);  
        };  
    }; 


3. 然后  project上右键  -》 ice builder   -》 add  ice  builder

则在项目下生成java代码

4. 然后建接口的实现


package com.stone.hello.testice.impl;

import com.stone.hello.testice._HelloDisp;

import Ice.Current;

public class HelloServiceImpl extends _HelloDisp {

    @Override
    public String sayHello(String s, Current __current) {
        // TODO Auto-generated method stub
        return "  这是我的 一个  zeroc ice  的测试 ";
    }

}

5. 建服务端  并  启动 

package ice_hello;

import com.stone.hello.testice.impl.HelloServiceImpl;  
 
public class Server {  
 
    public static void main(String[] args) {  
        int status = 0;  
        Ice.Communicator ic = null;  
        try{  
            System.out.println("Server starting...");  
            ic = Ice.Util.initialize(args);  
            Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("MyHelloAdapter", "default -p 9999");  
            Ice.Object object = new HelloServiceImpl();  
            adapter.add(object, ic.stringToIdentity("helloA"));  
            adapter.activate();  
            System.out.println("Server start success.");  
            ic.waitForShutdown();  
        }catch(Ice.LocalException e){  
            e.printStackTrace();  
            status = 1;  
        }catch(Exception e){  
            System.err.println(e.getMessage());  
            status = 1;  
        }  
        if(ic != null){  
            try{  
                ic.destroy();  
            }catch(Exception e){  
                System.err.println(e.getMessage());  
                status = 1;  
            }  
        }  
        System.exit(status);  
    }  
 
}  



6. 建客户端  并启动

package ice_hello;

import com.stone.hello.testice.HelloPrx;
import com.stone.hello.testice.HelloPrxHelper;

public class Client {  
      
    public static void main(String[] args) {  
        int status = 0;  
        Ice.Communicator ic = null;  
        try{  
            ic = Ice.Util.initialize(args);  
            Ice.ObjectPrx base = ic.stringToProxy("helloA:default -h localhost -p 9999");  
            HelloPrx hello = HelloPrxHelper.checkedCast(base);  
            if(hello == null){  
                throw new Error("Invalid proxy");  
            }  
            String s = hello.sayHello("World!");  
            System.out.println(">>"+s);  
        }catch(Ice.LocalException e){  
            e.printStackTrace();  
            status = 1;  
        }catch(Exception e){  
            System.err.println(e.getMessage());  
            status = 1;  
        }  
        if(ic != null){  
            try{  
                ic.destroy();  
            }catch(Exception e){  
                System.err.println(e.getMessage());  
                status = 1;  
            }  
        }  
        System.exit(status);  
    }  
 


控制台输出



demo下载地址 

http://download.youkuaiyun.com/detail/stonexmx/9544474


结束


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值