1

I have created a build.xml for compiling my source files and creating a jar. The jar is created when I run the build, but the jar runs with the following exception

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

The build file is :

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

How can I solve this?

andrewdotn
  • 32,721
  • 10
  • 101
  • 130
Gapchoos
  • 1,422
  • 5
  • 20
  • 40

1 Answers1

1

The error mentioned above is a java error, instead a jar error. In your classpath you have not included mysql-connector-java-5.1.*.jar, which is causing the script to fail.

NoClassDefFoundException is encountered when class existed at the time of compilation, but it is not found by application at the time of execution.

Change java task, and include classpath ref also... Hopefully this will work

<java jar="${jar.dir}/${ant.project.name}.jar" fork="true" classpathref="master-classpath"/>
Vishal
  • 3,189
  • 1
  • 15
  • 18
  • This doesnt solve the issue. The same exception occurs while creating the jar. – Gapchoos Dec 03 '12 at 05:10
  • Could you tell me the jar names in folders specified in the class path? – Vishal Dec 03 '12 at 06:39
  • It includes jar for mysql, Snmp4j and other external jars. These jars are included in ReleaseBuild/Libraries folder – Gapchoos Dec 03 '12 at 09:08
  • Can you test if this works even with eclipse? I think this jar is not required at the time of compiling the application. In this case your mysql jar is dependent on the jar specified above, but at the compile time this was not needed, as mysql is jar already built. But connector will be required while running the application. You will see same problem while running your application with another IDE i.e eclipse, net beans. You must download and add this jar in your ReleaseBuild/Libraries. – Vishal Dec 03 '12 at 09:41
  • I have done the same. but it shows same exception. the jar can be created in eclipse and it works fine – Gapchoos Dec 03 '12 at 10:00