JacORB requires JDK 1.6 or above properly installed on your machine. To build JacORB (and compile the examples) you need to have the XML–based make tool “Ant” installed on your machine (version 1.7.1 or greater). Ant can be downloaded from http://ant.apache.org. All make ?les (build.xml) are written for this tool.
将下载到的压缩包解压到任意目录下,Ant的配置非常简单,只需要将ant的安装目录添加到系统还击变量中即可。
计算机--属性--高级系统设置--环境变量,在系统变量中新建
1)ANT_HOME,输入ANT的安装目录;
2)在path目录中,添加:%ANT_HOME%\bin,
验证
在dos下输入ant命令,如果出现
说明ant配置成功!
注意:有时候输入ant命令会出现:
说明在: I:\Program Files\开发\Java JDK\lib\目录下缺少tools.jar文件,而这个tools.jar 可以在Java SE Development Kit 7u9安装目录下的lib目录下找到,直接将tools.jar 复制到JDK的安装目录下的lib目录下。
实例
在E 盘新建一个文件,命名为build.xml,内容为:
- <?xml version="1.0" encoding="GBK"?>
- <project name="验证脚本" default="copyfile" basedir="." >
- <target name="copyfile">
- <copy file="E:/test.txt" todir="F:/Temp" overwrite="true" />
- </target>
- </project>
则完成了文件复制功能!
3. 添加如下的系统环境变量
1) JACORB_HOME= C:\jacorb-3.3
2) Path环境变量添加%JACORB_HOME%/bin
3) CLASSPATH环境变量添加 C:\jacorb-3.3\lib;
根据官方文档的描述,C:\jacorb-3.3\lib下面主要含有下面几个jar包:
jacorb.jar - containing the ORB, IMR, IR and NamingService jacorb-services.jar - containing all other services (e.g. Noti?cation, DDS, Collection etc). idl.jar - containing the IDL compiler.
C:\jacorb-3.3\bin的命令ns 用于启动命名服务(Naming Service),测试是否可以正常启动,在命令行下输入ns(由于在系统环境变量path中已经添加了C:\jacorb-3.3\bin,所以此命令在任何目录中都可以调用),显示如下图:
![JacORB安装及配置 - hzh - The Legend of 1900 JacORB安装及配置 - hzh - The Legend of 1900](http://img1.ph.126.net/S65TQsaVLHC_NAhBC7xdYg==/1488158201969425363.png)
4. 配置JacORB
如官方文档描述:
JacORB has a number of con?guration options which can be set as Java properties. There are three options for setting properties:
- in properties ?les
- as command line properties, and
- as properties passed as arguments to ORB.init() in the code of your applications.
JacORB有很多的配置选项,他们都可以作为java中的properties来设置,有三种方式配置properties,分别是
通过properties文件,命令行参数,作为ORB.init()的参数传入
在下面的例子中,我使用的是第1种方式,即properties文件,将C:\jacorb-3.3\etc目录下的jacorb_properties.template复制一份仍然放在这个目录下,并改名为jacorb.properties,打开这个文件,将ORBInitRef.NameService=file:/c:/NS_Ref前的注释取消掉,这里要注意,由于我用的是win8系统,这个配置文件要在c盘下创建一个NS_Ref文件,由于win8的安全权限,导致这里一直出错,解决办法就是把C盘的权限提高到完全控制(之后在运行hello World程序的时候可以看到CORBA自动在c盘下建立了NS_Ref文件)
#ORBInitRef.NameService=corbaloc::160.45.110.41:38693/NameService ORBInitRef.NameService=file:/c:/NS_Ref #ORBInitRef.NameService=http://www.x.y.z/~user/NS_Ref #ORBInitRef.TradingService=http://www.x.y.z/~user/TraderRef
5. 实例 第一步要将IDL文件进行编译,使用JacORB的IDL编译器,在C:\jacorb-3.3\bin中打开idl.bat文件,里面内容如下:
@echo off set JACORB_HOME=%~dp0.. java -classpath "%JACORB_HOME%\lib\idl.jar;%CLASSPATH%" org.jacorb.idl.parser %*
红色部分说明了JacORB实际编译时的命令,我们可以模仿它的方式,编译相关的IDL文件(第一种方式),也可以直接使用C:\jacorb-3.3\bin中的idl.bat命令来编译(第二种方式) 在本例中IDL文件为HelloService.idl,内容如下:
module hello{ interface HelloService{ string sayHello(); oneway void shutdown(); }; };
在命令行中输入: java -cp "C:\jacorb-3.3\lib\idl.jar" org.jacorb.idl.parser HelloService.idl(第一种方式)
或者: idl -d . HelloService.idl (第二种方式)
这些生成的类,都是CORBA运行需要的,我们自己还需要编写一个实现了接口HelloService的实现类,以及客户端(上图中HelloClient)和服务器端(上图中HelloServer)代码分别如下:
//HelloService接口的实现类
package hello; import org.omg.CORBA.ORB; public class HelloServiceImpl extends HelloServicePOA{ private ORB orb ; public void setORB(ORB orb_val){ orb = orb_val ; } public String sayHello(){ return "\nHello World!!!\n"; } public void shutdown(){ orb.shutdown(false); } }
//服务器端
package hello; import org.omg.CORBA.ORB; import org.omg.CosNaming.NameComponent; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; import org.omg.PortableServer.POA; import org.omg.PortableServer.POAHelper; public class HelloServer { public static void main(String[] args) { try { // 创建和初始化ORB ORB orb = ORB.init(args, null); System.out.println("server--->11111"); // 获得根POA的引用,并且激活POAManager POA rootpoa = POAHelper.narrow(orb .resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); System.out.println("server--->22222"); // 创建一个HelloServiceImpl对象,并且把它与ORB关联 HelloServiceImpl helloServiceImpl = new HelloServiceImpl(); System.out.println("server--->33333"); helloServiceImpl.setORB(orb); // 获得HelloServiceImpl对象的CORBA类型的对象引用 org.omg.CORBA.Object ref = rootpoa .servant_to_reference(helloServiceImpl); HelloService href = HelloServiceHelper.narrow(ref); System.out.println("server--->44444"); // 获得代表命名服务的Context org.omg.CORBA.Object objRef = orb .resolve_initial_references("NameService"); NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); System.out.println("server--->55555"); // 把HelloService对象与HelloService名字绑定 String name = "Helloservice"; NameComponent path[] = ncRef.to_name(name); ncRef.rebind(path, href); System.out.println("server--->66666"); System.out.println("HelloServer ready and wating......"); //等待客户端访问HelloService对象 orb.run(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("HelloServer Exiting..........."); } }
//客户端
package hello; import org.omg.CORBA.ORB; import org.omg.CosNaming.NamingContextExt; import org.omg.CosNaming.NamingContextExtHelper; public class HelloClient { static HelloService helloServiceImpl ; public static void main(String[] args) { try { // 创建和初始化ORB ORB orb = ORB.init(args, null); // 获得代表命名服务的Context org.omg.CORBA.Object objRef = orb .resolve_initial_references("NameService"); NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); // 获得名为"HelloService"的HelloService对象的远程引用 String name = "Helloservice"; helloServiceImpl = HelloServiceHelper.narrow(ncRef .resolve_str(name)); //调用HelloService对象的远程方法 System.out.println("obtained a handle on server object:" + helloServiceImpl); System.out.println(helloServiceImpl.sayHello()); helloServiceImpl.shutdown(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
![JacORB安装及配置 - hzh - The Legend of 1900 JacORB安装及配置 - hzh - The Legend of 1900](http://img1.ph.126.net/_m3v_8_ey5L1UGCiSpERCQ==/3333508149284770495.png)
![JacORB安装及配置 - hzh - The Legend of 1900 JacORB安装及配置 - hzh - The Legend of 1900](http://img2.ph.126.net/qCc2W_emJ7sv1lW-Lgiugg==/669066019659143651.png)
![JacORB安装及配置 - hzh - The Legend of 1900 JacORB安装及配置 - hzh - The Legend of 1900](http://img2.ph.126.net/mK3OIkHsUAMba_aJQtvRXA==/26740122805559870.png)
![JacORB安装及配置 - hzh - The Legend of 1900 JacORB安装及配置 - hzh - The Legend of 1900](http://img2.ph.126.net/yB08z7R_VEPcPbPWXvUdEg==/2972094279187820090.png)
![JacORB安装及配置 - hzh - The Legend of 1900 JacORB安装及配置 - hzh - The Legend of 1900](http://img2.ph.126.net/BpXfH58sHlcLYMScyq7PUQ==/1983272686003496255.png)