Netbeans平台项目 - 具有依赖关系的包装jar文件的问题

时间:2010-03-29 17:41:30

标签: java netbeans6.8 apache-commons-beanutils

对于初学者来说,这个问题并不是在NetBeans IDE中编程开发NetBeans项目(例如使用NetBeans平台框架)。

我正在尝试使用BeanUtils库来内省我的域模型,并提供要在属性表中显示的属性。示例代码:

public class MyNode extends AbstractNode implements PropertyChangeListener {

    private static final PropertyUtilsBean bean = new PropertyUtilsBean();

    // snip

    protected Sheet createSheet() {

        Sheet sheet = Sheet.createDefault();
        Sheet.Set set = Sheet.createPropertiesSet();

        APIObject obj = getLookup().lookup (APIObject.class);

        PropertyDescriptor[] descriptors = bean.getPropertyDescriptors(obj);

        for (PropertyDescriptor d : descriptors) {


            Method readMethod = d.getReadMethod();
            Method writeMethod = d.getWriteMethod();
            Class valueType = d.getClass();
            Property p = new PropertySupport.Reflection(obj, valueType, readMethod, writeMethod);

            set.put(p);

        }
        sheet.put(set);
        return sheet;
}

我在commons-beanutils-1.8.3.jar周围创建了一个包装器模块,并在包含上述代码的模块中添加了对模块的依赖。一切都很好。当我尝试运行程序并打开属性表视图(即上面的代码实际运行)时,我收到以下错误:

java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
    at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:259)
Caused: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory starting from ModuleCL@64e48e45[org.apache.commons.beanutils] with possible defining loaders [ModuleCL@75da931b[org.netbeans.libs.commons_logging]] and declared parents []
    at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:261)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:399)
Caused: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.commons.beanutils.PropertyUtilsBean.<init>(PropertyUtilsBean.java:132)
    at org.myorg.myeditor.MyNode.<clinit>(MyNode.java:35)
    at org.myorg.myeditor.MyEditor.<init>(MyEditor.java:33)
    at org.myorg.myeditor.OpenEditorAction.actionPerformed(OpenEditorAction.java:13)
    at org.openide.awt.AlwaysEnabledAction$1.run(AlwaysEnabledAction.java:139)
    at org.netbeans.modules.openide.util.ActionsBridge.implPerformAction(ActionsBridge.java:83)
    at org.netbeans.modules.openide.util.ActionsBridge.doPerformAction(ActionsBridge.java:67)
    at org.openide.awt.AlwaysEnabledAction.actionPerformed(AlwaysEnabledAction.java:142)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
    at com.apple.laf.ScreenMenuItem.actionPerformed(ScreenMenuItem.java:95)
    at java.awt.MenuItem.processActionEvent(MenuItem.java:627)
    at java.awt.MenuItem.processEvent(MenuItem.java:586)
    at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:317)
    at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:305)
[catch] at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:125)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我知道beanutils正在使用commons-logging组件。我尝试以两种不同的方式添加commons-logging组件(围绕commons-logging库创建一个包装器库,并依赖于Commons Logging Integration库)。

都没有解决问题。

我注意到其他包装库也会出现同样的问题;如果它们本身具有外部依赖关系,那么ClassNotFoundExceptions会疯狂地传播,即使我已经将它们所需的库包装起来并将它们作为依赖项添加到原始包装的库模块中。

图示地:

alt text http://files.droplr.com/files/18876491/IwG2.NetBeans%20Project%20problem.png

我在这里结束了我的智慧。谷歌搜索时我发现了类似的问题:

Is there a known bug on NB Module dependency

Same issue I'm facing but when wrapping a different jar

NetBeans stance on this - none of the 3 apply to me.

没有最终帮助我。

谢谢,

尼克

编辑:我设法通过向beanutils库包装器添加commons-logging和commons-collections的依赖项来获取beanutils的编译示例。但我的问题仍然存在于其他情况。

2 个答案:

答案 0 :(得分:2)

我重建了罐子,重新封装了它们,并修复了ClassPathNotFound异常。

答案 1 :(得分:0)

Caused: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.commons.beanutils.PropertyUtilsBean.<init>(PropertyUtilsBean.java:132)

此错误基本上暗示Commons Logging需要与Commons Beanutils位于同一个类路径中。我不确定你在创建包装器模块时的意思以及为什么要这样做,但通常你只是将库(JAR文件)放在项目文件夹中,该文件夹由项目的构建路径(也就是类路径)覆盖,或者手动将JAR文件添加到项目的构建路径中。