Java 为什么我没有通过Com4J接收COM事件?
我正在使用Com4J与Microsoft Outlook进行交互。我已经根据生成了Java类型定义。下面是一个等待用户关闭电子邮件的代码示例Java 为什么我没有通过Com4J接收COM事件?,java,events,com,com4j,Java,Events,Com,Com4j,我正在使用Com4J与Microsoft Outlook进行交互。我已经根据生成了Java类型定义。下面是一个等待用户关闭电子邮件的代码示例 // Registers my event handler mailItem.advise( ItemEvents.class, new ItemEvents() { @Override public void close(Holder<Boolean> cance
// Registers my event handler
mailItem.advise(
ItemEvents.class,
new ItemEvents() {
@Override
public void close(Holder<Boolean> cancel) {
// TODO Auto-generated method stub
super.close(cancel);
System.out.println("Closed");
}
}
);
// Displays the email to the user
mailItem.display();
//注册我的事件处理程序
mailItem.advision(
ItemEvents.class,
新项目事件(){
@凌驾
公共作废关闭(持有人取消){
//TODO自动生成的方法存根
超级。关闭(取消);
系统输出打印项次(“关闭”);
}
}
);
//向用户显示电子邮件
mailItem.display();
此代码成功地向用户显示电子邮件。不幸的是,当用户关闭窗口时,我的程序从不打印
“Closed”
。当Com4J生成事件类时(ItemEvents
,在我的场景中),所有生成的方法的默认行为都是抛出UnsupportedOperationException
(有关详细信息,请参阅com4j.tlbimp.EventInterfaceGenerator
类)
例如,下面是我的匿名类覆盖的ItemEvents
类的close
方法:
@DISPID(61444)
public void close(Holder<Boolean> cancel) {
throw new UnsupportedOperationException();
}
令我惊讶的是,Com4J似乎完全忽略了从事件处理程序抛出的UnsupportedOperationException
,没有留下任何实际发生的迹象。我编写此代码是为了演示:
mailItem.advise(
ItemEvents.class,
new ItemEvents() {
@Override
public void close(Holder<Boolean> cancel) {
System.out.println("Getting ready to throw the exception...");
throw new RuntimeException("ERROR! ERROR!");
}
}
);
mailItem.advision(
ItemEvents.class,
新项目事件(){
@凌驾
公共作废关闭(持有人取消){
System.out.println(“准备抛出异常…”);
抛出新的RuntimeException(“ERROR!ERROR!”);
}
}
);
程序发出以下输出:
Getting ready to throw the exception...
正在准备抛出异常。。。
但是,没有迹象表明曾经抛出过运行时异常
Getting ready to throw the exception...