为什么我没有通过 Com4J 接收 COM 事件?
我正在使用 Com4J 与 Microsoft Outlook 交互。我已经按照 Com4J 教程生成了 Java 类型定义。以下是等待用户关闭电子邮件的一些代码示例。
// 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();
此代码成功向用户显示电子邮件。不幸的是,当用户关闭窗口时,我的程序永远不会打印“Closed”。
I am using Com4J to interact with Microsoft Outlook. I have generated the Java type definitions as per the Com4J tutorial. Here is an example of some code that waits for the user to close an email.
// 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();
This code successfully displays the email to the user. Unfortunately, my program never prints "Closed"
when the user closes the window.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 Com4J 生成事件类(在我的场景中为
ItemEvents
)时,所有生成方法的默认行为是抛出UnsupportedOperationException
(请参阅com4j.tlbimp.EventInterfaceGenerator
类了解详细信息)。例如,下面是我的匿名类重写的
ItemEvents
类的close
方法:因此,当我的匿名类调用
super.close(cancel);< /code>,父类抛出
UnsupportedOperationException
,阻止执行到达我的System.out.println("Closed");
语句。因此,我的匿名类应该确实看起来像这样:令我惊讶的是,Com4J 似乎只是忽略了从事件中抛出的
UnsupportedOperationException
完全处理程序,让我没有任何迹象表明实际发生了什么。我编写此代码是为了演示:程序发出此输出:
但是,没有任何迹象表明曾经抛出过 RuntimeException。
When Com4J generates an event class (
ItemEvents
in my scenario), the default behavior for all generated methods is to throw anUnsupportedOperationException
(see thecom4j.tlbimp.EventInterfaceGenerator
class for details).For example, here is the
close
method of theItemEvents
class that my anonymous class overrides:Therefore, when my anonymous class calls
super.close(cancel);
, the parent class throws anUnsupportedOperationException
, preventing execution from reaching mySystem.out.println("Closed");
statement. Therefore, my anonymous class should really have looked like this:What surprised me is that Com4J appears to have simply ignored the
UnsupportedOperationException
thrown from the event handler altogether, leaving me no indication of what actually happened. I wrote this code to demonstrate:The program emits this output:
However, there is no indication that a
RuntimeException
was ever thrown.