Java调用com组件之jacob

一、背景介绍

现有标准的 win32 com组件,有如下的参数:

属性 值 说明
Program IDyinhai.yh_hb_sctr
COM ClassID
COM ClassName
COClass_yh_hb_sctr

Interface TypeDual Interface
Interface NameIyh_hb_sctr
具有一个方法:
yh_hb_call( string astr_jyhb, string astr_jysr, ref string astr_jysc)
需要使用Java进行调用。

二、采用jacob进行调用

2.1 添加依赖

<dependency>
  <groupId>com.hynnet</groupId>
  <artifactId>jacob</artifactId>
  <version>1.18</version>
</dependency>

 2.2 将dll文件放置在java_home\bin目录中

package com.msip;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class TestCom {
    public static void main(String[] args) {
        ComThread.InitSTA(false);
        ActiveXComponent component = new ActiveXComponent("yinhai.yh_hb_sctr");

        try {
            // 获取 COM 组件的 Dispatch 对象
            Dispatch dispatch = (Dispatch) component.getObject();
            String rest = null ;
            String input = "{}";
            // 直接调用方法
            Variant result = Dispatch.invoke(dispatch, "yh_hb_call", Dispatch.Method,
                    new Object[]{"1101", input,rest}, new int[1]);
            // 获取方法调用的结果
            System.out.println("Result: " + result.toString());
            System.out.println("rest="+rest);
        } finally {
            // 关闭 COM 组件,释放资源
            component.safeRelease();
        }

    }
}

三、遇到的问题

3.1 jdk版本问题,文中的com组件是32位系统的,所以程序的jdk必须也是32位的。

3.2 配套的dll程序必须在类路径中

3.3 代码中的try finally语法块是必须的,否则,机会提示找不到对应的方法名。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值