未找到(编辑:问题就解决了 - 请参照最后的细节)类在OSGi的使用SwingUtilities.invokeLater时捆绑
我想在一个OSGi包来创建Swing JFrame
与WindowAdapter
。当我使用SwingUtilities.invokeLater
执行此操作时,找不到WindowAdapter
类。没有invokeLater
它的作品。
我该怎么做才能在使用invokeLater
时发现WindowAdapter
? 在OSGi环境中是不是invokeLater
不合适?
细节:
我开始与我的自定义启动,安装软件包并启动Apache的菲利克斯框架实例。我的包的启动方法是这样的:
public void start(BundleContext arg0) throws Exception {
myFrame = new MyFrame();
myFrame.open();
}
这是MyFrame类:
public class MyFrame {
JFrame mainFrame;
public void open() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
openImpl();
}
});
// If called like this it works:
// openImpl();
}
public void openImpl() {
mainFrame = new JFrame("Title");
mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
WindowAdapter wa = new WindowAdapter() {
};
mainFrame.addWindowListener(wa);
mainFrame.setSize(800, 600);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
}
这是我表现的包:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_03-b05 (Oracle Corporation)
Built-By: Rainer Schwarze
Bundle-Name: DummyBdl
Bundle-Description: Dummy Bundle
Bundle-Vendor: admaDIC
Bundle-Version: 0.0.1
Bundle-Activator: dummybdl.Activator
Import-Package: org.osgi.framework, javax.swing
Export-Package: dummybdl.api
Export-Service: dummybdl.Provider
这是堆栈跟踪这我得到:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1432)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at dummybdl.MyFrame.openImpl(MyFrame.java:24)
at dummybdl.MyFrame$1.run(MyFrame.java:16)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
作为一名OSGi新手,我尝试了几件事来解决它,但到目前为止找不到解决方案。那么,为什么不把我的第一个问题,在StackOverflow上:-)
编辑:事实证明,这个问题完全是我的
调试半小时后: 我的代码停止openImpl
之前OSGi框架被称为EDT。
因此Apache Felix框架将BundleWiringImpl
实例(请参阅堆栈跟踪)标记为已处置。当我的openImpl
在EDT中被调用时,BundleWiringImpl.getClassLoader
返回null
,因为它被标记为已处理。最终这导致NPE。 (我应该去张贴50行我菲利克斯装载机的可能做出错误明显的额外步骤。)
如果代码是准确的,那么行MyFrame.java:24是“new JFrame()”,所以无法加载的类大概是javax.swing.JFrame。我真的被这个难住了。我想了很长一段时间...... –
如果我有写jar版本,那么BundleWiringImpl的第1432行是这样的:http://grepcode.com/file/repo1.maven.org/maven2/org。 apache.felix/org.apache.felix.framework/4.0.2/org/apache/felix/framework/BundleWiringImpl.java?av = h#1432,这意味着getClassLoader()返回null或m_revision为null。 –