If you have created your JAR the first step is done.

Starting the JAR could be done in several ways. Common is that you have to have 
all external classes on the runtime classpath:

 

1. Hard coded start script.

Write a bash/bat-Script with the java command with all cp settings, e.g. (bat)

@echo off

java -cp /build/myjar.jar;lib/one.jar;lib/two.jar;lib/three.jar org.acme.Main %*

 

2. Wrapper script which collects all JARs in a dynamic way (see ant.bat|ant.sh)

 

3. Use Ant + <java><classpath> for starting

 

4. Create a runnable JAR which references the external JARs (manifest: 
main-class + classpath)

 

5. Create a shaded jar (uber jar, fat jar): include all classes from external 
jars into your jar

 

6. Use a launcher which uses a dependency manager for getting the classpath

 

 

 

You don't have to replicate path definitions in your buildfile, you could (and 
should) use references:

<javac><classpath id="runtime.cp"><fileset dir="lib" includes="**/*.jar"/>…

<java><classpath refid="runtime.cp"/>

 

 

You mave have a look at 
http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

 

 

Jan

 

 

Von: Dennis Putnam [mailto:d...@bellsouth.net] 
Gesendet: Dienstag, 19. Februar 2019 15:40
An: user@ant.apache.org
Betreff: Re: Javac Run By Ant Script is Unable to Find External Jars

 

Hi Jaikiran,

Thanks for the reply. Here is my command:

/bin/ant -v -Dmember.number=$number -Dmember.name=\"$name\" -f makejar.xml

I assume it is using ant's java task based on the build file (makejar.xml). I'm 
a bit confused. Doesn't all the necessary classpath info come from the build 
xml? Since the Maven pom.xml is never referenced anywhere in the build file and 
the Maven dependency causes that exception, it seems to me that is where the 
problem lies. Doesn't the pom.xml need to be referenced somewhere?

In any case, how do I set up a classpath for the ant command (-lib) since I 
have multiple paths? Do I have to copy all my jars into a single directory? Why 
is duplication of these paths even necessary? This seems like it is getting far 
afield since I did not have to do all that before I messed up the build file 
classpath.

On 2/18/2019 11:03 PM, Jaikiran Pai wrote:

Hi Dennis,
 
I think you are almost there to get this working. How are you running
this Java application? Are you using Ant's java task? What does it look
like? Just like you did with javac, the java task will need to know what
classpath to use while running the application. As long as you setup
your classpath to contain all these necessary jar files, you should no
longer see those exceptions.
 
-Jaikiran
 
On 18/02/19 8:15 PM, Dennis Putnam wrote:

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
 
 

 

 

 

Reply via email to