使用jacob过程中遇到的问题

java 专栏收录该内容
20 篇文章 0 订阅

使用jacob过程中遇到的问题

JACOB一个Java-COM中间件.通过这个组件你可以在Java应用程序中调用COM组件和Win32,win64程序库。
jacob 就是 JAVA-COM Bridge的缩写,它是在java与微软的com组件之间的桥梁,通过使用jacob自带的dll动态链接库通过JNI的方式实现了在sun java平台上的程序对com调用。

可以在window上实现语音播报功能。不啰嗦,直接上代码。

使用方法

  1. 把jacob.jar加载到工程里;
  2. 把jacob.dll放入 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin目录下;
  3. 把jacob.dll放入 C:\WINDOWS\system32\下;

代码

package com.example.demo;

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

public class Jocab {
public static void main(String[] args) {
ActiveXComponent ax = null;
// String str=“请A001号到3号窗口”;
// String str=“华为 new bee”;
String str = “3号机柜报警,3号机柜报警,3号机柜报警”;
try {
ax = new ActiveXComponent(“Sapi.SpVoice”);

        //运行时输出语音内容
        Dispatch spVoice = ax.getObject();
        // 音量 0-100
        ax.setProperty("Volume", new Variant(100));
        // 语音朗读速度 -10 到 +10
        ax.setProperty("Rate", new Variant(0));
        // 执行朗读
        Dispatch.call(spVoice, "Speak", new Variant(str));

        //下面是构建文件流把生成语音文件

        ax = new ActiveXComponent("Sapi.SpFileStream");
        Dispatch spFileStream = ax.getObject();

        ax = new ActiveXComponent("Sapi.SpAudioFormat");
        Dispatch spAudioFormat = ax.getObject();
        //设置音频流格式
        Dispatch.put(spAudioFormat, "Type", new Variant(22));
        //设置文件输出流格式
        Dispatch.putRef(spFileStream, "Format", spAudioFormat);
        //调用输出 文件流打开方法,创建一个.wav文件
        Dispatch.call(spFileStream, "Open", new Variant("F:\\test.wav"), new Variant(3), new Variant(true));
        //设置声音对象的音频输出流为输出文件对象
        Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
        //设置音量 0到100
        Dispatch.put(spVoice, "Volume", new Variant(100));
        //设置朗读速度
        Dispatch.put(spVoice, "Rate", new Variant(0));
        //开始朗读
        Dispatch.call(spVoice, "Speak", new Variant(str));

        //关闭输出文件
        Dispatch.call(spFileStream, "Close");
        Dispatch.putRef(spVoice, "AudioOutputStream", null);

        spAudioFormat.safeRelease();
        spFileStream.safeRelease();
        spVoice.safeRelease();
        ax.safeRelease();

    } catch (Exception e) { e.printStackTrace();
    }
}

}

---------------------------------------------------------------------

使用过程中遇到的问题

常见错误解释:

1、找不到jacob.dll,配置路径问题(确认jacob.dll配置正确)

Exception in thread “……” java.lang.UnsatisfiedLinkError: no jacob in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at com.jacob.com.Dispatch.(Dispatch.java:36)
……
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

2、找不到资源文件,资源文件路径问题(查看程序中资源文件路径的设置,并检查资源文件的存放路径)

Exception in thread “……” com.jacob.com.ComFailException: Invoke of: Open
Source: Microsoft Word
Description: RCHITECTURE=x86
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:890)
at com.jacob.com.Dispatch.callN(Dispatch.java:526)
at com.jacob.com.Dispatch.call(Dispatch.java:606)
at WordControl.openDocument(Test06_08_01.java:3027)
……
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

3、jacob.dll或jacob.jar对应版本错误

com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Documents
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.change(WordToHtml.java:73)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.main(WordToHtml.java:121)
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Quit
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.change(WordToHtml.java:92)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.main(WordToHtml.java:121)
Exception in thread “main”

  • 0
    点赞
  • 3
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值