0

This is my pom.xml file of the application

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.smart.data.manipulator</groupId>
    <artifactId>SmartDataManipulator</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SmartDataManipulator</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Dependency for Logger -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- Dependency for MySQL JDBC Driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>SmartDataManipulator</finalName>
        <plugins>
            <!-- Set a JDK compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <!-- Make this jar executable -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- Jar file entry point -->
                            <mainClass>com.smart.data.manipulator.Starter</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- Copy project dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I have setup all the required stuff to make my jar file executable.

Following is the error I get once I run the mvn package command on the terminal. I use Ubuntu as my OS and IdeaJ as my IDE.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at com.smart.data.manipulator.Starter.<clinit>(Starter.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

I know that this exception indicates that Log4J dependency is not existing. But It is there in the dependency folder.

junit-3.8.1.jar  log4j-1.2.17.jar  mysql-connector-java-5.1.34.jar
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51

1 Answers1

0

As it stands, your MANIFEST.MF file does not tell the JVM where to look for libraries, and that is why you get the exception java.lang.NoClassDefFoundError. This is because there is no ClassPath section inside the manifest.

The goals of the MANIFEST.MF file is to store the fully classified name of the main class and the location to all the libraries needed to make the application work.

In a Maven project, this file is typically generated by the maven-jar-plugin. You need to configure the maven-jar-plugin this way:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <!-- Jar file entry point -->
                <mainClass>com.smart.data.manipulator.Starter</mainClass>
                <!-- Adds a ClassPath section to the manifest -->                    
                <addClasspath>true</addClasspath>
                <!-- All the dependencies are stored inside the dependency folder, as created by default by the maven-dependency-plugin -->
                <classpathPrefix>dependency/</classpathPrefix> 
            </manifest>
        </archive>
    </configuration>
</plugin>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • 1
    Hi .. It was able to over that exception .. ^_^ but still I get this error : Error: Could not find or load main class jar – Du-Lacoste Mar 05 '15 at 08:52