带有 Ant 的 Java NoClassDefFoundError [英] Java NoClassDefFoundError with Ant

查看:22
本文介绍了带有 Ant 的 Java NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 res/lib 文件夹中有一个第三方 .jar 文件.ANT build.xml 看起来像这样:

I have a third-party .jar file in a res/lib folder. The ANT build.xml looks like this:

<?xml version="1.0"?>
<project name="my.project" basedir="." default="build">
    <property name="src.dir" value="src"/>
    <property name="build.dir" value="build/classes"/>

    <path id="master-classpath">
        <fileset dir="res/lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${build.dir}"/>
    </path>

    <target name="build">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}" optimize="true">
            <src path="${src.dir}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>
</project>

.java 文件如下:

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

public class IO {
    public static void readCSVFile(File file) throws IOException {
        FileReader in = new FileReader(file);
        Iterable<CSVRecord> record = CSVFormat.DEFAULT.parse(in);
    }
}

编译没问题,但出现运行时错误:java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat.我认为类路径有问题,但对我来说似乎没问题.

The compile is OK, but I got a runtime error: java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat. I think something is wrong with the classpath, but it seems ok to me.

更新:

如果我使用 java -cp path; 它正在运行.主要.我试图编写一个 ANT 脚本来运行它:

It is running if I use java -cp path;. Main. I have tried to write an ANT script to run it:

<target name="run">
    <java classname="Main">
        <classpath refid="master-classpath"/>
    </java>
</target>

我在命令行中写了 ant run,我得到 BUILD SUCCESSFUL 并没有任何反应.

I write ant run in the command line, I get BUILD SUCCESSFUL and nothing happens.

推荐答案

您的构建文件编译您的代码并将第三方 Jar 包含在类路径中,以便编译器能够定位类您的代码所依赖的(例如 org.apache.commons.csv.CSVRecord).

Your buildfile compiles your code and includes the third-party Jar in the classpath so that the compiler could be able to locate the classes on which your code depends (e.g. org.apache.commons.csv.CSVRecord).

类似地,当通过 java运行您的主类时,您的 JVM 需要知道第三方类的存在位置.否则类加载器将无法加载这些类.因此,在您的示例中,您仍应按如下方式调用主类:

Similarly, when running your main class via the java your JVM needs to know where the third-party classes exist. Otherwise the classloader will fail to load these classes. So in your example, you should still call the main class as follows:

java -cp pathtoyourlib;. Main

换句话说,这是两个不同的类路径"概念:一个类路径被编译器用来知道在哪里定位每个要编译的类中的每个引用的类,而另一个被 JVM 用来知道在哪里在动态调用类时加载类.

In other words, these are two different "classpath" concepts: one classpath is used by the compiler to know where to locate every referenced class in every class to be compiled, while the other is used by the JVM to know where to load classes when they are dynamically invoked.

这篇关于带有 Ant 的 Java NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆