Search code examples
javaexceptionmaven-2program-entry-pointapache-commons-config

After mvn install I try to run main, but I get a NoClassDefFoundError in ConfigurationException


I created a maven project and I have written for weeks an application and want to run it in the end... But I can't.

I get the following stacktrace when executing my .jar:

C:\Users\MyUser\.m2\repository\com\myCompany\MyApp\
0.0.1-SNAPSHOT> java -Dconfigfile=config.xml -jar MyApp-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/configuration/ConfigurationException
        at com.mycompany.connector.DBConnector.reloadDBSettings(DBConnector.java:23)
        at com.mycompany.connector.DBConnector.<clinit>(DBConnector.java:16)
        at com.mycompany.connector.DBConnectorFactory.createDBConnector(DBConnectorFactory.java:17)
        at com.mycompany.service.MyAppService.<clinit>(MyAppService.java:38)
        at com.mycompany.main.MyAppMain.init(MyAppMain.java:13)
        at com.mycompany.main.MyAppMain.main(MyAppMain.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.configuration.ConfigurationException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more

org.apache.commons.configuration.ConfigurationException is imported to the class and I set up Maven to use it as dependency. What am I doing wrong?

BTW: this is my reloadDBSettings Method:

import java.io.FileNotFoundException;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public final class ConfigurationHandler {

    private static XMLConfiguration config;
/**
     * to reload Configuration-File if necessary
     */
    public static void reloadConfig() throws FileNotFoundException {
            try {
                config = new XMLConfiguration(System.getProperty("configfile"));
            } catch (ConfigurationException e) {
                throw new FileNotFoundException("Es wurde keine Konfiguration geladen - Überprüfen Sie die Systemvariable 'configfile'(" + e.getMessage() + ")");
            }
    }
}

Solution

  • ClassNotFoundException means JVM cannot find the class in your classpath. This is different from NoClassDefFoundError which means the classes use by that class are not found (not the class itself).

    There are two ways to solve this problem:

    1. add all the dependent jar files to a folder, add that to JVM classpath and run your main class using:

      java -cp .\dependency\*;MyApp-0.0.1-SNAPSHOT.jar.jar com.myCompany.MyApp.Main

      and in order to get all the dependencies into a folder you can use maven dependencies plugin maven-dependency-plugin

    2. use maven assembly plugin maven-assembly-plugin. This plugin will bundle all the dependent jar files in to a signle jar file so you can execute it the same way you do. However, sometime this method does not work especially for a larger application that use a lot of libraries. The jars may conflict each other so you may need to resolve the dependencies yourself or use Tattletale maven plugin to solve it for you.

    Note: Tattletale is also good to solve conflict in the first case.