1

我创建了一个 build.xml 来编译我的源文件并创建一个 jar。当我运行构建时创建了 jar,但是 jar 运行时出现以下异常

 [java] Exception in thread "main" java.lang.NoClassDefFoundError: com/mysql
/jdbc/exceptions/MySQLIntegrityConstraintViolationException

构建文件是:

<?xml version="1.0"?>
<project name="NmzAzzist" basedir="." default="main">
    <property name="src.dir" value="../Source/NMSAzzist/src" />
    <property name="build.dir" value="ReleaseBuild/classes" />
    <property name="jar.dir" value="ReleaseBuild/ReleaseJars" />

    <path id="master-classpath">
        <fileset dir="Libraries">
            <include name="*.jar" />
        </fileset>

    </path>

    <patternset id="meta.files">
        <include name="**/*.xml" />
        <include name="**/*.properties" />
        <include name="**/*.mib" />
    </patternset>

    <!-- Simply extends the compile.classpath with your own compiled classes. -->

    <target name="clean" description="Clean output directories">
        <delete dir="${build.dir}" />
    </target>

    <target name="build" description="Compile source tree java files">
        <echo>Compiling the source code</echo>
        <mkdir dir="${build.dir}" />
        <javac destdir="${build.dir}" source="1.5" target="1.5"
            includeantruntime="false">
            <src path="${src.dir}" />
            <classpath refid="master-classpath" />
        </javac>
    </target>

    <target name="copy.meta.files">
        <copy todir="${build.dir}">
            <fileset dir="${src.dir}">
                <patternset refid="meta.files" />
            </fileset>
        </copy>
    </target>

    <target name="jar" depends="build, copy.meta.files">
        <mkdir dir="${jar.dir}" />
        <echo>building jar!</echo>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-Class"
                    value="com.ushustech.nmsazzist.NMSAzzistApp" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true" />
    </target>
    <target name="main" depends="clean,run" />


</project>

我该如何解决这个问题?

4

1 回答 1

1

上面提到的错误是java错误,而不是jar错误。在您的类路径中,您没有包含mysql-connector-java-5.1.*.jar,这导致脚本失败。

NoClassDefFoundException 在编译时存在类时遇到,但在执行时应用程序找不到。

更改 java 任务,并包括 classpath ref ......希望这会奏效

<java jar="${jar.dir}/${ant.project.name}.jar" fork="true" classpathref="master-classpath"/>
于 2012-11-30T16:09:12.110 回答