Search code examples
javaapachejarapache-poiivy

IVY/JAR ERROR - java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row


Hi I know this question has been asked multiple times but I have tried all of those suggested!

My code works perfectly fine in Eclipse however I want to build jar and run through command line in Unix.

In my dependent ivy.xml I have listed my dependencies:

    <dependencies>
              <dependency org="org.apache.xmlbeans" name="xmlbeans" rev="2.6.0"/>
              <dependency org="org.apache.poi" name="poi" rev="3.11" conf="default" /> 
              <dependency org="org.apache.poi" name="poi-ooxml" rev="3.11" conf="default" />
              <dependency org="org.apache.poi" name="poi-ooxml-schemas" rev="3.11" conf="default" />
    </dependencies>

But when I execute my jar, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row

I know you need to have poi-ooxml for ss, however, I have referenced that in my ivy dependencies.

Why am I still getting this error?

Any help would be greatly appreciated.


Solution

  • I'm guessing your jar is missing a classpath in it's manifest. Try the following:

    <target name="build" depends="test" description="Create executable jar archive">
        <ivy:retrieve pattern="${dist.dir}/lib/[artifact]-[revision](-[classifier]).[ext]"/>
    
        <manifestclasspath property="jar.classpath" jarfile="${jar.file}">
            <classpath>
                <fileset dir="${dist.dir}/lib" includes="*.jar"/>
            </classpath>
        </manifestclasspath>
    
        <jar destfile="${jar.file}" basedir="${build.dir}/classes">
            <manifest>
                <attribute name="Main-Class" value="${jar.main.class}" />
                <attribute name="Class-Path" value="${jar.classpath}" />
            </manifest>
        </jar>
    </target>
    

    Notes:

    • The ivy retrieve task is used to place the dependencies into the distribution directory alongside the jar being built
    • The manifestclasspath task is a useful trick for creating the correct relative paths for a possibly long list of dependencies