未找到(編輯:問題就解決了 - 請參照最後的細節)類在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。 –