Uploaded image for project: 'Maven'
  1. Maven
  2. MNG-2550

ClassNotFoundException and NoClassDefFoundError when running plugin

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • 2.0.4
    • None
    • Plugins and Lifecycle
    • None

    Description

      I created a plugin which runs a small application which does some parsing, logging and serializing. When running the plugin I get ClassNotFoundExceptions and NoClassDefFoundErrors.

      The application used BeanUtils (1.7). Somewhere in that package a Log instance is created, but the classloader cannot find org.apache.log4j.Logger and an NoClassDefFoundError is thrown.
      The application serializes objects to a file using the ObjectOutputStream and ObjectInputStream. Serializing to a file works fine. Serializing from file to an Object throws a ClassNotFoundException because the base classloader does not know any object from any dependency.

      Both errors are caused by the classloader not knowing any of the dependend packages of the Maven project in which the plugin is used or on which the plugin depends.
      I tried several solutions: using my own URLClassLoader extention and using ClassWorld and creating a new ClassRealm. To both I added all project.RuntimeClasspathElements.
      These solutions fixed some problems but not all. In some cases the original classloader is still used (by included packages like org.apache.common.logging or by base java classes like java.io.ObjectInputStream) and that classloaded does not know my project classes and dependend packages.

      Can't maven itself not just include all dependend packages of the project (and plugin) on the classpath so you don't have to do that yourself inside the plugin ?
      I searched for similar bugs and found some but they are all fixed and closed in older maven version.
      Can someone please help me with this issue ?

      Here part of the stacktrace. As you can see, the ObjectInputStream uses Class.forName() which resolves to the RealmClassLoader, but not to my MyURLClassLoader instance.

      java.lang.ClassNotFoundException: nl.eid.aegon.rpf.parser.model.ClassDescription
      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:306)
      at org.codehaus.classworlds.RealmClassLoader.loadClassDirect(RealmClassLoader.java:195)
      at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:255)
      at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:274)
      at org.codehaus.classworlds.DefaultClassRealm.loadClass(DefaultClassRealm.java:274)
      at org.codehaus.classworlds.RealmClassLoader.loadClass(RealmClassLoader.java:214)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:242)
      at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:584)
      at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1543)
      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1465)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1698)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1304)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349)

      Here the stacktrace for the NoClassDefFoundError. It starts when I try to log something in the application using org.apache.common.logging. The only way I could solve this was to put the log4j package in the Maven lib directory, but this is not a prefered solution. The log4j.jar is a dependend package and should be included on the classpath by the classloader.

      [INFO] ------------------------------------------------------------------------
      [INFO] Trace
      java.lang.ExceptionInInitializerError
      at nl.eid.replatforming_framework.parser.Parser.parse(Parser.java:45)
      at nl.eid.replatforming_framework.maven.RunAllMojo.runAll(RunAllMojo.java:404)
      at nl.eid.replatforming_framework.maven.RunAllMojo.execute(RunAllMojo.java:139)
      at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:412)
      at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:534)
      at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:488)
      at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:458)
      at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:306)
      at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:273)
      at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:140)
      at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:322)
      at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:115)
      at org.apache.maven.cli.MavenCli.main(MavenCli.java:256)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
      at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
      at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
      at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
      Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@9260ee for org.apache.commons.logging.impl.Log4JLogger
      at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:532)
      at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:272)
      at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:246)
      at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:395)
      at nl.eid.replatforming_framework.config.Configuration.<clinit>(Configuration.java:37)
      ... 21 more
      Caused by: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@9260ee for org.apache.commons.logging.impl.Log4JLogger
      at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:432)
      at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:525)
      ... 25 more
      Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
      at java.lang.Class.getDeclaredConstructors0(Native Method)
      at java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
      at java.lang.Class.getConstructor0(Class.java:2640)
      at java.lang.Class.getConstructor(Class.java:1629)
      at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:429)
      ... 26 more
      [INFO] ------------------------------------------------------------------------

      Attachments

        Activity

          People

            brett Brett Porter
            jdboer Joost den Boer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: