4

I have a build file that declares the classpath as shown

<path id="compile.classpath">
    <fileset dir="${basedir}/lib"    includes="**"/>        
    <fileset dir="${jboss.home}/lib" includes="**"/>        
    <pathelement path ="${build.classes.dir}"/>
</path>

I tried looking at the documentation but I am not able to understand the use of pathelement.

I know that the ID is used to refer to this class path while performing a task and fileset includes the jarfiles.

edit 1: My doubt is Why can't we use fileset to include the class files in place of pathelement?

Shurmajee
  • 1,027
  • 3
  • 12
  • 35
  • ANT manual: http://ant.apache.org/manual/using.html#path – Mark O'Connor Jan 21 '13 at 18:06
  • 2
    The manual does not explain pathelement in depth – Shurmajee Jan 22 '13 at 05:11
  • Agreed. The documentation makes assumptions that the reader understand Java classpaths and classloaders. It also doesn't properly explain how the "path" tag relates to "classpath". They're much the same thing but the doco is pretty poor for beginners IMHO – Mark O'Connor Jan 22 '13 at 08:47

2 Answers2

6

Latest edit:

My doubt is Why can't we use fileset to include the class files in place of pathelement?

If you use a fileset then you'd be adding a set of class files to the path as follows:

CLASSPATH=classes/MyClass1.class:classes/MyClass2.class:classes/MyClass3.class:....

When what Java expects to see is simply:

CLASSPATH=classes

Only jar (and WAR,EAR,etc) files are explicitly listed on the classpath (Java will open them up and load their class files), hence the need for a fileset in ANT.

Update

Here's the Oracle documentation:

Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:

  • For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
  • For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
  • For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • I agree so in my example why cant I replace with – Shurmajee Jan 22 '13 at 08:53
  • 1
    You missed my point.... Using a fileset adds each class file to the classpath... This is not how Java expects the classpath to be populated. For class files all it needs is the root directory – Mark O'Connor Jan 22 '13 at 08:57
2

There was already similar question about 'pathelements' here. From the provided documentation: "If it's path structure like in your example: "A path-like structure can include a reference to another path-like structure (a path being itself a resource collection) via nested elements"

 <path id="base.path">
  <pathelement path="${classpath}"/>
  <fileset dir="lib">
    <include name="**/*.jar"/>
  </fileset>
  <pathelement location="classes"/>
</path>

If it's classpath structure: "The path attribute is intended to be used with predefined paths"

<classpath>
  <pathelement path="${classpath}"/>
  <pathelement location="lib/helper.jar"/>
</classpath>
Community
  • 1
  • 1
alsid
  • 490
  • 6
  • 14
  • 1
    Yes, there is a bit of uncertainty in understanding these pathelement. And it's not covered in documentation. But have you tried to search about the difference between fileset and path? For example, there is other question: [link](http://stackoverflow.com/questions/6544571/ant-nested-paths-vs-fileset) – alsid Jan 22 '13 at 07:51
  • Well I had a look at the link and many other questions and it seems that people always use pathelement for their class files. – Shurmajee Jan 22 '13 at 08:33