PARITOSH THAPLIYAL
PARITOSH THAPLIYAL

Reputation: 51

jar build using maven showing error

i have build an application that fetch the value from Count tag of some xml files and writes it to the output excel XLSX file.

i have 2 functions in my program which are called by main(). First is readXml() which read the xml files and second is writeXlsx() which writes to the output excel file.

i have used maven to add dependencies and build jar file.

when i run the created jar, it works well for half of the program but gives an error when try to write XLSX. Error comes up is :

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
    at com.mpstddn.App.writeXLSX(App.java:107)
    at com.mpstddn.App.main(App.java:64)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook

same program works well when i run it from eclipse but gives error when i make a jar of it and run it. It seems to me that jar is not able to include the apache poi lib. I am new to maven and don't know where i am making mistake. here is my pom.xml :

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mpstddn.java</groupId>
    <artifactId>myMavenProject</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>myMaven</name>
    <url>http://maven.apache.org</url>
    <dependencies>
       <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
        </exclusions>
      </dependency>
      <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>openxml4j</artifactId>
          <version>1.0-beta</version>
      </dependency>     
       <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.10-FINAL</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
     </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <scriptSourceDirectory>src\main\scripts</scriptSourceDirectory>
        <testSourceDirectory>src\test\java</testSourceDirectory>
        <outputDirectory>target\classes</outputDirectory>
        <testOutputDirectory>target\test-classes</testOutputDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>lib</directory>
            <excludes>
                <exclude>**/*.java</exclude>
                <exclude>**/*.jar</exclude>
            </excludes>
        </resource>
 </resources>
 <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.0</version>
            </plugin>
        </plugins>
 </pluginManagement>
  <plugins>
        <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                        <archive>
                                <manifest>
                                        <mainClass>com.mpstddn.App</mainClass>
                                </manifest>
                        </archive>
                </configuration>
            </plugin>
       <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <executions>
                <execution>
                    <id>default-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <id>default-clean</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>default-install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>install</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-testResources</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testResources</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <executions>
                  <execution>
                    <id>default-test</id>
                      <phase>test</phase>
                     <goals>
                        <goal>test</goal>
                     </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
             <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                     <manifest>
                         <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                             <mainClass>com.mpstddn.App</mainClass>
                      </manifest>
                 </archive>
            </configuration>
             <executions>
                <execution>
                     <id>default-jar</id>
                    <phase>package</phase>
                    <configuration>
                        <packagingExcludes>*.jar</packagingExcludes>
                    </configuration>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
          <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
            <executions>
                <execution>
                    <id>default-site</id>
                    <phase>site</phase>
                    <goals>
                        <goal>site</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target\site</outputDirectory>
                        <reportPlugins>
                            <reportPlugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                            </reportPlugin>
                        </reportPlugins>
                    </configuration>
                </execution>
                <execution>
                    <id>default-deploy</id>
                    <phase>site-deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target\site</outputDirectory>
                        <reportPlugins>
                            <reportPlugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                            </reportPlugin>
                        </reportPlugins>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <outputDirectory>target\site</outputDirectory>
                <reportPlugins>
                    <reportPlugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                    </reportPlugin>
                </reportPlugins>
            </configuration>
        </plugin>
       </plugins>
     </build>
 </project>

The way I create the jar : right click on project in eclipse -> run as -> Maven install this create a jar file in target directory inside project folder.

I am running the jar file using command prompt by typing :

java -jar myMavenProject-0.0.1-SNAPSHOT.jar X:\input_directory input.xml 9

Upvotes: 4

Views: 1944

Answers (1)

khmarbaise
khmarbaise

Reputation: 97517

So your problem is not based on a dependency it's because you configured maven-assembly-plugin wrong:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
           <id>package</id>
           <goals>
             <goal>single</goal>
           <goals>
           <phase>package</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
 </build>
</project>

The problem is that maven-assembly-plugin does not bind by default to any life cycle phase. Afterwards try:

mvn clean package

And check the resulting jar file (target/WhatEver-x.x-SNAPSHOT-jar-with-dependencies.jar).

Upvotes: 1

Related Questions