1

So I wrote an ant build.xml file where I take the class files from two Java programs, one that extends the other, package them up into two separate jar files, and then, launches them.

<java classname="Main">
             <classpath>
                  <pathelement location="${mainDir}"/>
                  <pathelement path="${Main-Class}"/>
             </classpath>
       </java>

Whenever I invoke ant, it says that "Main" can not be found. I can post the rest of the build.xml file if needed but it's really just this part that I'm confused about. I'm pretty sure that I have the classname right but my biggest problem is figuring out what goes in for location and path. Right now I just have dummy variables.

EDIT: Here's the whole file.

<?xml version="1.0"?>
<project default="dist" name="webscarab">
<description>Class</description>
<property name="ClassFiles" location="..\Simple\trunk\dev\Class\bin\" />
<property name="mainClassFiles" location="..\Simple\trunk\dev\main\build\" />
<property name="buildDir" location=".\build" />
<property name="distDir" location=".\dist" />
<property name="mainDir" location="..\Simple\trunk\dev\webscarab\src\" />
<target name="init">
        <tstamp/>
        <mkdir dir="${buildDir}"/>
        <mkdir dir="${distDir}"/>
</target>
<target name="dist" depends="init">
    <jar destfile="${distDir}/package${DSTAMP}.jar" basedir="${ ClassFiles}"/>
    <jar destfile="${distDir}/package-web-${DSTAMP}.jar" basedir="${mainClassFiles}"/>
    <java classname="Main">
     <classpath>
       <pathelement location="${mainDir}"/>
       <pathelement path="${Main-Class}"/>
     </classpath>
   </java>
</target>
</project>

EDIT 2: I should mention everything is already compiled and I have all the .class files. It's not something I need to do in the Ant file.

0

4 Answers 4

1

The error message about “class cannot be found” is likely due to an incorrect classpath.

The classpath should grant access to all the class and resource files your application requires. It is composed of a set of directories and jar files. location will denote a single directory or path, whereas path will denote multiple, separated by ; or : depending on your platform.

In your case, it seems to me that you want your classpath to either consist of ${pegaFuzzClassFiles} and ${webScarabClassFiles} as directories, or of ${distDir}/package-pega-${DSTAMP}.jar and ${distDir}/package-web-${DSTAMP}.jar as jar files. You could do this using a single <pathelement path="…"/> element, but I'd suggest multiple <pathelement location="…"/> as I consider this to be clearer.

You should also make sure that any third-party libraries used by your application are available on that path as well. You could nest one or more <fileset> into that <classpath>, each of them describing all the jar files in a given directory.

Sign up to request clarification or add additional context in comments.

Comments

1

You probably need to change the elements to point to the Jars that are being created, right now the classpath would appear to point to the source, which is not going to work as the classpath needs the compiled .class files or the jars that contain them.

I would also, just as a matter of style, move the task into a separate 'run' or 'run-app' target that depends on the dist target.

2 Comments

Yeah I'll separate them, I just wanted to get this working first. So under location would I put the directory where the jar files live and then what would I put under path? Or am I still mixed up.
Something like <pathelement location="${distDir}/package-pega-${DSTAMP}.jar"/> should do the trick.
0

You need to Javac task to compile your code and after that you can run your compiled class. For more information about Javac task visit http://ant.apache.org/manual/Tasks/javac.html

The second advice: The use of the unix slash '/' is strongly recommended, whether you're on windows or not. Change your mainDir,distDir and buildDir property locations.

Comments

0

I don't have a ton of ant experience, but you might need to explicitly tell java which jar file to run.

<java jar="path-to-jar-file" classname="org.owasp.webscarab.Main">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.