Tuesday, December 26, 2017

Apache Ant modifies classpath

Just a quick note - I have discovered this thing while digging into Java class loading / reflection. When running my test program from command line, like so:
java -ea -cp ./build/jarclassscanning.jar my.package.jarclassscanning.BaseClass
(yes, I specify my main class directly and just add my jar file to the classpath)

my classpath contains what I expect it to contain:
/home/me/projects/jarclassscanning/build/jarclassscanning.jar

But if I use my simplistic ant build.xml, which contains target run:
<target name="run">
  <java classname="my.package.jarclassscanning.BaseClass">
    <arg value="-ea"/>
    <classpath>
      <pathelement location="./build/jarclassscanning.jar"/>
    </classpath>
  </java>
</target>

this is what my classpath becomes (printed out from the test program itself):
/home/me/apps/apache-ant-1.9.7/lib/ant-launcher.jar

However, if I add fork="true" to the java task (just as you should if you use jar attribute, which I don't):
<target name="run">
  <java classname="my.package.jarclassscanning.BaseClass" fork="true">
    <arg value="-ea"/>
    <classpath>
      <pathelement location="./build/jarclassscanning.jar"/>
    </classpath>
  </java>
</target>

everything's back to normal:
/home/me/projects/jarclassscanning/build/jarclassscanning.jar

No comments:

Post a Comment