我在Java和Netbeans平台编程方面非常新鲜,我对10 Netbeans API(文件系统)中的一个示例有一个非常奇怪的问题。 我使用的是Ubuntu 14.10,Java JDK 8,Netbeans 8.0.1
我想在点击菜单元素后在swing消息框中显示一些数据。进口很好,dep libs很好,所有编译都很好。但是,当我点击我的高级菜单项时,我有例外:
java.lang.ClassNotFoundException: javax.swing.JOptionPane not found by org.netbeans.word.module.fsdemo [42] at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532) at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75) at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955) Caused: java.lang.ClassNotFoundException: *** Class 'javax.swing.JOptionPane' was not found because bundle org.netbeans.word.module.fsdemo [42] does not import 'javax.swing' even though bundle org.apache.felix.framework [0] does export it. Additionally, the class is also available from the system class loader. There are two fixes: 1) Add an import for 'javax.swing' to bundle org.netbeans.word.module.fsdemo [42]; imports are necessary for each class directly touched by bundle code or indirectly touched, such as super classes if their methods are used. 2) Add package 'javax.swing' to the 'org.osgi.framework.bootdelegation' property; a library or VM bug can cause classes to be loaded by the wrong class loader. The first approach is preferable for preserving modularity. *** at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1968) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) Caused: java.lang.NoClassDefFoundError: javax/swing/JOptionPane at org.netbeans.word.module.fsdemo.MenuViewer.actionPerformed(MenuViewer.java:33) at org.openide.awt.AlwaysEnabledAction$1.run(AlwaysEnabledAction.java:199) at org.openide.util.actions.ActionInvoker$1.run(ActionInvoker.java:95) at org.openide.util.actions.ActionInvoker.doPerformAction(ActionInvoker.java:116) at org.openide.util.actions.ActionInvoker.invokeAction(ActionInvoker.java:99) at org.openide.awt.AlwaysEnabledAction.actionPerformed(AlwaysEnabledAction.java:202) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.AbstractButton.doClick(AbstractButton.java:376) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877) at java.awt.Component.processMouseEvent(Component.java:6525) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6290) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159) [catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我正在尝试导入所有javax.spring。*包,但它根本不起作用。 有趣,因为在其他类型的模块类(如windows等)中,相同的代码工作正常。 这是我要查看的花哨代码:
package org.netbeans.word.module.fsdemo; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.awt.ActionRegistration; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.util.NbBundle.Messages; @ActionID( category = "Edit", id = "org.netbeans.word.module.fsdemo.MenuViewer" ) @ActionRegistration( displayName = "#CTL_MenuViewer" ) @ActionReference(path = "Menu/Edit", position = 2600, separatorBefore = 2550, separatorAfter = 2650) @Messages("CTL_MenuViewer=Viewer") public final class MenuViewer implements ActionListener { private FileObject root; private FileObject dir; @Override public void actionPerformed(ActionEvent event) { root = FileUtil.getConfigRoot(); dir = root.getFileObject("Menu"); for (FileObject oneItem : dir.getChildren()) { JOptionPane.showMessageDialog(null, "Info", oneItem.getName(), JOptionPane.INFORMATION_MESSAGE); } } }
答案 0 :(得分:3)
这是因为正在使用OSGi框架。 OSGi是模块化系统的标准,其中模块(由一些(版本化的)罐组成的“捆绑”)可以与其他模块的依赖性隔离加载。它具有生命周期管理和模块分离 - 通过特定的类加载器实现。
这是一个比java提供的更严格的模块化系统,具有可见性和版本化依赖关系。一些功能最终将被用于Java。
因此为swing添加一个bundle依赖项。不幸的是,我目前没有NetBeans IDE来检查。