【问题标题】:ivy eclipse NoClassDefFoundError: org/apache/commons/lang3/StringUtils常春藤日食 NoClassDefFoundError: org/apache/commons/lang3/StringUtils
【发布时间】:2018-04-03 01:13:33
【问题描述】:

我知道这个问题已经解决了,但没有一个答案能解决我的问题。我也是这方面的新手。 我确实得到 NoClassDefFoundError: org/apache/commons/lang3/StringUtils

我可以看到它解决了依赖关系,但是当我在编译后运行 jar 时,我找不到类。

这是我的 ivy.xml 文件

<configurations>
        <conf name="runtime" description="Runtime"/>
</configurations>

<dependencies>
    <dependency org="org.apache.commons" name="commons-lang3" rev="3.6" conf="runtime->default"/>
    <dependency org="junit" name="junit" rev="4.12" conf="runtime->default"/>
</dependencies>

下面是我的 build.xml

<project name="HelloWorld-build" basedir="." default="clean-deploy" xmlns:ivy="antlib:org.apache.ivy.ant">
    <property name="src.dir" location="src" />
    <property name="build.dir" location="build" />
    <property name="dist.dir" location="dist" />

    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <target name="init" description="resolve dependencies with ivy">
        <ivy:resolve />
        <ivy:cachepath pathid="default.classpath" conf="runtime" />
    </target>

    <target name="compile" depends="init">
        <mkdir dir="${build.dir}/classes" />
        <javac srcdir="${src.dir}" destdir="${build.dir}/classes" includeantruntime="false">
            <classpath refid="default.classpath" />
        </javac>
    </target>

    <target name="package" depends="compile">
        <mkdir dir="${dist.dir}" />
        <jar destfile="${dist.dir}/HelloWorld.jar" basedir="${build.dir}/classes">
            <manifest>
                <attribute name="Main-Class" value="mypackage.HelloWorld" />
            </manifest>
        </jar>
    </target>

    <target name="deploy" depends="package">
        <java jar="${dist.dir}/HelloWorld.jar" fork="true">
        </java>
    </target>
</project>

我在运行部署目标时遇到问题。当我手动运行我的 java 应用程序(不使用 build.xml)时,它工作正常。 请帮我解决这个问题。

这是java文件

import org.apache.commons.lang3.StringUtils;

public class HelloWorld {
    public static void main(String[] args) {
        String message = "Hello world!";
        if (!StringUtils.isEmpty(message)) {
            System.out.println(message);
        }
    }
}

【问题讨论】:

    标签: build noclassdeffounderror ivy apache-stringutils


    【解决方案1】:

    NoClassDefFound 错误被抛出,因为 commons-lang3 在运行时类路径中不可用。您仅在 javac 中将它作为类路径提供,这意味着它仅在编译阶段可用。

    由于这是一个未与您的 jar 打包的外部 jar,因此它必须在运行时 claspath 中可用。但是当使用 jar 属性时,Ant 会忽略所有类路径设置。

    要解决此问题,您可以如下更改部署目标

    <target name="deploy" depends="package">
        <java classname="mypackage.HelloWorld" fork="true">
            <classpath>
                <path refid="default.classpath"/>
                <path location="${dist.dir}/HelloWorld.jar"/>
            </classpath>
        </java>
    </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 2021-05-20
      • 2018-05-02
      • 2017-04-18
      • 2016-12-28
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多