Re: Javac Run By Ant Script is Unable to Find External Jars

Dennis Putnam <[email protected]>
Newsgroups gmane.comp.jakarta.ant.user
Message-ID <[email protected]>
Hi Matt and Jan,

Thanks to both of you as I am closer but I think I am still missing a
piece. I am now getting a successful build but the resulting jar is not
finding the classes in the external jars. When I run the jar I get this
exception:

    Exception in thread "main" java.lang.NoClassDefFoundError:
    org/apache/http/HttpEntity
            at KCBSEvents.Registration.isRegistered(Unknown Source)
            at KCBSEvents.KCBSEvents.main(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 2 more


That happens to be the first attempt to execute one of the external jar
classes which is a Maven dependency. Here is my new build.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="KCBSEvents" default="jar" basedir=".">
            <property name="build.properties" value="build.properties"/>
            <property name="resources" value="resource" />
            <property name="jardir" value="KCBSEvents" />
            <property name="KCBSDir" value="src/KCBSEvents" />
            <property name="member.number" value="000000" />
            <property name="member.name" value="" />
           *<property name="jarpath"
    value="/${user.home}/.m2/repository" />**
    **        <path id="compile-jars">**
    **                <multirootfileset
    basedirs="${jarpath}/commons-io/2.5,${jarpath}/httpcomponents-client,${jarpath}/commons-logging/1.2,${jarpath}/commons-codec/1.10,/lib/java-ext/json-simple-1.1.1">**
    **                        <include name="commons-io-2.5.jar" />**
    **                        <include name="httpclient-4.5.6.jar" />**
    **                        <include name="httpcore-4.4.10.jar" />**
    **                        <include name="commons-logging-1.2.jar" />**
    **                        <include name="commons-codec-1.10.jar" />**
    **                        <include name="json-simple-1.1.1.jar" />**
    **                </multirootfileset>**
    **        </path>*
            <target name="checkOS">
                    <condition property="isWindows">
                            <os family="windows" />
                    </condition>
                    <condition property="isLinux">
                            <os family="unix" />
                    </condition>
            </target>
            <target name="if_windows" depends="checkOS" if="isWindows">
                    <property name="jarfile"
    value="C:\temp\KCBSEvents.jar" />
                    <property name="antcontrib"
    value="H:\html\Applets\ant-contrib" />
            </target>
            <target name="if_linux" depends="checkOS" if="isLinux">
                    <property name="jarfile"
    value="/tmp/${member.number}/KCBSEvents.jar" />
                    <property name="antcontrib"
    value="/var/www/html/Applets/ant-contrib/ant-contrib-1.0b3.jar" />
            </target>
            <target name="setclass" depends="if_linux,if_windows">
                    <taskdef
    resource="net/sf/antcontrib/antcontrib.properties">
                            <classpath>
                                    <pathelement location="${antcontrib}" />
                            </classpath>
                    </taskdef>
            </target>
            <target name="incserial" depends="setclass">
                    <copy todir="bin/${jardir}/${resources}">
                            <fileset dir="${KCBSDir}/${resources}">
                                    <include name="${build.properties}" />
                            </fileset>
                            <filterchain>
                                    <expandproperties />
                            </filterchain>
                    </copy>
                    <if> <isset Property="build.number" /> <then>
                            <echo message="update build requested" />
                    </then> <else>
                            <echo message="new build requested" />
                            <buildnumber />
                    </else> </if>
                    <propertyfile
    file="bin/${jardir}/${resources}/${build.properties}">
                            <entry key="serialnumber"
    value="${build.number}" />
                            <entry key="membernumber"
    value="${member.number}" />
                            <entry key="name" value="${member.name}" />
                    </propertyfile>
                    <echo message="serial number: ${build.number}" />
            </target>
            <target name="jar" description="Compile serialized jar"
    depends="incserial,if_windows,if_linux">
                    <echo message="Using destination file ${jarfile}" />
                    <javac srcdir="src" destdir="bin"
    includeantruntime="false" *classpathref="compile-jars"* />
                    <jar destfile="${jarfile}" basedir="bin"
    filesetmanifest="mergewithoutmain">
                            <manifest>
                                    <attribute name="Manifest-Version"
    value="1.0"/>
                                    <attribute name="Created-By"
    value="ant 1.9.2 on CentOS 7" />
                                    <attribute name="Main-Class"
    value="KCBSEvents.KCBSEvents" />
                            </manifest>
                            *<!-- zipfilesets removed -->*
                    </jar>
            </target>
    </project>

Does this come back to my question about needing the zipfileset elements
for the Maven jars? I don't know how the pom.xml for Maven gets pulled
into this either.

On 2/17/2019 12:45 PM, Matt Bertolini wrote:
> Hi Dennis,
>
> I think you might be mixing up Eclipse concepts and Ant concepts and that
> might be causing some extra confusion. Based on your original email, the
> compiler is having issues finding your third-party dependencies. I believe
> Jaikiran is correct in saying that the <javac> task needs to be given some
> sort of classpath. The .classpath file is an Eclipse concept and unless
> there is some sort of Eclipse/Ant plugin I am not aware of, the .classpath
> file will have no effect on Ant’s, <javac> task. Also, I don’t believe the
> <javac> task has a default value for the classpath attribute. If no
> classpath or classpathref is specified then the compiler is not passed any
> classpath information.
>
> I would start by locating the folder where your third-party dependencies
> are and create a <path> element containing them like this:
>
> <path id="compile-classpath">
>     <fileset dir="/the/path/to/your/third/party/jars"/>
> </path>
>
> Once you have a path defined you can pass the classpath to the <javac> task
> using a refid like this:
>
> <javac srcdir="src" destdir="bin" includeantruntime="false"
> classpathref="compile-classpath"/>
>
> Try that and see how it goes.
>
> Matt Bertolini
>
>
signature.asc (application/pgp-signature, 196 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)

iEYEARECAAYFAlxqxQMACgkQ/fiWYqZ2tVT3SwCgmSthFaz5H+n1GKRC0KFkAc2A
n/QAn07HG/I8CWhrcCkxMnr3R0greuIy
=qlJL
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.