Java的NoClassDefFoundError的使用Ant

Java的NoClassDefFoundError的使用Ant

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

问题描述

我有一个 RES / lib目录文件夹中的第三方的.jar 文件。蚂蚁的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的:组织/阿帕奇/公/ 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命令路径;.主要。我试图写一个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>

我写蚂蚁运行在命令行中,我得到 BUILD SUCCESSFUL 并没有任何反应。

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

推荐答案

您构建文件中的编译的您code和包含在类路径中的第三方jar,这样编译器可以能定位上的$ C $,c取决于(如 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

在换句话说,这是两个不同的类路径的概念:一是classpath中所使用的编译器知道在哪里可以找到每类每一个引用的类进行编译,而另一种是用由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.

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

08-24 02:31