【问题标题】:IVY/JAR ERROR - java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/RowIVY/JAR 错误 - java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
【发布时间】:2016-03-29 04:06:36
【问题描述】:

您好,我知道这个问题已被问过多次,但我已经尝试了所有建议!

我的代码在 Eclipse 中运行良好,但是我想构建 jar 并在 Unix 中通过命令行运行。

在我的依赖 ivy.xml 中,我列出了我的依赖项:

    <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>

但是当我执行我的 jar 时,我收到以下错误:

线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/poi/ss/usermodel/Row

我知道您需要为 ss 提供 poi-ooxml,但是,我已经在我的 ivy 依赖项中引用了它。

为什么我仍然收到此错误?

任何帮助将不胜感激。

【问题讨论】:

  • 您是否确保包含对两个编译时间运行时类路径的依赖项?

标签: java apache jar apache-poi ivy


【解决方案1】:

我猜你的 jar 在它的清单中缺少一个类路径。请尝试以下操作:

<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>

注意事项:

  • ivy retrieve 任务用于将依赖项放入正在构建的 jar 旁边的分发目录中
  • ma​​nifestclasspath 任务是一个有用的技巧,可以为可能很长的依赖项列表创建正确的相对路径

【讨论】:

  • 我终于弄明白了,我的作品的默认版本是 3.6(真的很旧),而我使用的是较新的版本,显然在我们的环境中不起作用。
  • @anirana93 好的...但是使用 ivy 管理依赖项的全部意义在于使您的构建独立于它所运行的环境(我称之为“魔术构建服务器”问题)。管理类路径也需要在运行时完成。这就是为什么在上面的示例中,我概述了如何使用 ivy 在您正在构建的 jar 中设置正确的类路径。只需运行命令“java -jar myapp.jar”
猜你喜欢
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 2019-06-14
  • 2011-09-21
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多