title: Java ant jar task & class path issues
body: <h3>---------Edit-----</h3>
  
  <p>So I've now come to realize that my errors are stemming from a command line launch rather than launching the jars by clicking on them. Essentially if I run the jars from any folder but the folder they actually reside in then my resources are incorrectly referenced. If I command line execute from the same folder this issue goes away.
  So the question becomes what can I do to reference the jar and resources relatively, regardless of where the execution is called from?</p>
  
  <h3>--------Edit------</h3>
  
  <p>I'm having several issues trying to get a project built for distribution. The project structure is such that I have 6 packages. Each package is split into two sub packages Gui and Engine. Each sub package has a runnable main class. The "Common" package is a wrapper for the other packages and launches whichever gets selected. My goal is to jar each package up into a standalone for the engine side of things. This is used for command line testing of the engine. The "Common" jar gets put together with all resources and runs the overall application. </p>
  
  <p>My issue stems from a need to clean the project up. So re-factoring and repackaging are being done however the method used currently to build and jar the project is a batch script that copies class files(respecting package/folder structure) to the end folder location. From there the jars are created command line, and the class path and main class are outlined in a manifest file. This works just fine, the jars compile with the correct structure and classpath and everything runs correctly. However its a pain to keep the xcopy commands correct as it needs to be changed to whatever the package structure gets changed to, along with location depending on who is building the project.</p>
  
  <p>So my solution was to use a build.xml and outline ant build for netbeans. This would respect any project structure changes and keep the build internal to netbeans.  The exact issue I'm having is that regardless of the build method used.(Batch / Build script) I get the exact folder structure within the jars using either method, and the classpath is exactly the same. My common.jar works fine for the project, but my testing jars for the engine run into classpath or reference issues. I'll include the majority of the ant script, batch file, and the manifest file used.</p>
  
  <p>Batch file</p>
  
  <pre><code>xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Common  C:\Emerson\Common\
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Common\gui C:\Emerson\Common\gui\
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Common\Meg C:\Emerson\Common\Meg\
  
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Pressu re C:\Emerson\Pressure\
  xcopy /Y  C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Pressure\Gui C:\Emerson\Pressure\Gui\
  xcopy /Y  C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Pressure\Meg C:\Emerson\Pressure\Meg\
  
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\PressureV2 C:\Emerson\PressureV2\
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\PressureV2\Gui C:\Emerson\PressureV2\Gui\
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\PressureV2\Gui\controller C:\Emerson\PressureV2\Gui\controller
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\PressureV2\Gui\models C:\Emerson\PressureV2\Gui\models
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\PressureV2\Gui\rules C:\Emerson\PressureV2\Gui\rules
  
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Temp\Gui C:\Emerson\Temp\Gui\
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\Temp\Meg C:\Emerson\Temp\Meg\
  
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\rfdFlow\Gui C:\Emerson\rfdFlow\Gui\
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\rfdFlow\Meg C:\Emerson\rfdFlow\Meg\
  
  xcopy /Y C:\Users\tfollansbee\Documents\NetBeansProjects\Emerson\branches\Merge\build\classes\utils C:\Emerson\utils\
  
  "C:\Program Files\Java\jdk1.6.0_25\bin\jar" cvmf mainClass tempAsynch.jar Temp/*
  "C:\Program Files\Java\jdk1.6.0_25\bin\jar" cvmf mainClass Common.jar Common/*
  "C:\Program Files\Java\jdk1.6.0_25\bin\jar" cvmf pressureClass Pressure.jar Pressure/*
  "C:\Program Files\Java\jdk1.6.0_25\bin\jar" cvmf pressureV2Class PressureV2.jar  PressureV2/*
  "C:\Program Files\Java\jdk1.6.0_25\bin\jar" cvmf rfdClass rfdFlow.jar rfdFlow/*
  "C:\Program Files\Java\jdk1.6.0_25\bin\jar" cvmf pressureV2Class utils.jar utils/*
  
  rmdir /s /q C:\Emerson\Common
  rmdir /s /q C:\Emerson\Pressure
  rmdir /s /q C:\Emerson\PressureV2
  rmdir /s /q C:\Emerson\Temp
  rmdir /s /q C:\Emerson\rfdFlow
  rmdir /s /q C:\Emerson\utils
  
  xcopy /Y C:\Emerson\Common.jar C:\GCCDEV
  xcopy /Y C:\Emerson\tempAsynch.jar C:\GCCDEV
  xcopy /Y C:\Emerson\Pressure.jar C:\GCCDEV
  xcopy /Y C:\Emerson\PressureV2.jar C:\GCCDEV
  xcopy /Y C:\Emerson\rfdFlow.jar C:\GCCDEV
  xcopy /Y C:\Emerson\utils.jar C:\GCCDEV
  </code></pre>
  
  <p>Build script</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project name="Emerson" default="default" basedir="."&gt;
  &lt;description&gt;Builds, tests, and runs the project Emerson.&lt;/description&gt;
  &lt;property environment="env"/&gt;
  &lt;property name="baseDir" value="build/classes/" /&gt;
  &lt;property name="cp" value=" . utils.jar lib/pfcasync.jar lib/poi-3.7/poi-ooxml-schemas-3.7-20101029.jar lib/poi-3.7/poi-3.7-20101029.jar lib/poi-3.7/poi-ooxml-3.7-20101029.jar lib/poi-3.7/ooxml-lib/xmlbeans-2.3.0.jar lib/poi-3.7/ooxml-lib/dom4j-1.6.1.jar lib/xom/xom-1.2.7.jar lib/jgoodies/jgoodies-common-1.3.0.jar lib/jgoodies/jgoodies-binding-2.6.0.jar lib/resources/** Common.jar 
  " /&gt;
  &lt;import file="nbproject/build-impl.xml"/&gt;
  &lt;target name="-pre-jar"&gt;
      &lt;echo message="-pre-jar" /&gt;
      &lt;delete dir="${env.PTC_STD}/lib/" /&gt;
      &lt;delete dir="${env.PTC_STD}/resources/" /&gt;
      &lt;!--delete dir="${env.PTC_STD}/build/" /--&gt;
      &lt;echo message="aJar" /&gt;
      &lt;jar destfile="${env.PTC_STD}/Common.jar" basedir="${baseDir}"&gt;
          &lt;manifest&gt;
              &lt;attribute name="Class-Path" value="${cp}" /&gt;
              &lt;attribute name="Main-Class" value="Common.gui.emersonWrapper" /&gt;
          &lt;/manifest&gt;
      &lt;/jar&gt;
      &lt;jar destfile="${env.PTC_STD}/tempAsynch.jar" index="false" basedir="${baseDir}" includes="Temp/**"&gt;
          &lt;manifest&gt;
              &lt;attribute name="Class-Path" value="${cp}" /&gt;
              &lt;attribute name="Main-Class" value="Temp.Meg.tempasynch" /&gt;
          &lt;/manifest&gt;
      &lt;/jar&gt;
      &lt;jar destfile="${env.PTC_STD}/Pressure.jar" index="false" basedir="${baseDir}" includes="Pressure/**"&gt;
          &lt;manifest&gt;
              &lt;attribute name="Class-Path" value="${cp}" /&gt;
              &lt;attribute name="Main-Class" value="Pressure.Meg.P3051STest" /&gt;
          &lt;/manifest&gt;
      &lt;/jar&gt;
      &lt;jar destfile="${env.PTC_STD}/PressureV2.jar" index="false"&gt;
          &lt;fileset dir="${baseDir}"&gt;
              &lt;include name="PressureV2/**" /&gt;
              &lt;include name="utils/**" /&gt;
          &lt;/fileset&gt;
          &lt;manifest&gt;
              &lt;attribute name="Class-Path" value="${cp}" /&gt;
              &lt;attribute name="Main-Class" value="PressureV2.Gui.PressureV2Main" /&gt;
          &lt;/manifest&gt;
      &lt;/jar&gt;
      &lt;jar destfile="${env.PTC_STD}/rfdFlow.jar" index="false" basedir="${baseDir}" includes="rfdFlow/**"&gt;
          &lt;manifest&gt;
              &lt;attribute name="Main-Class" value="rfdFlow.Meg.main" /&gt;
              &lt;attribute name="Class-Path" value="${cp}" /&gt;
          &lt;/manifest&gt;
      &lt;/jar&gt;
      &lt;jar destfile="${env.PTC_STD}/utils.jar" index="false" basedir="build/classes/" includes="utils/**"&gt;
          &lt;manifest&gt;
              &lt;attribute name="Class-Path" value="${cp}" /&gt;
              &lt;attribute name="Main-Class" value="PressureV2.Gui.PressureV2Main" /&gt;
          &lt;/manifest&gt;
      &lt;/jar&gt;
      &lt;copy todir="C:/Emerson/resources/"&gt;
          &lt;fileset dir="resources/"&gt;
              &lt;exclude name="**/*.svn"/&gt;
          &lt;/fileset&gt;
      &lt;/copy&gt;
      &lt;copy todir="C:/Emerson/lib/"&gt;
          &lt;fileset dir="lib/"&gt;
              &lt;include name="**/*.jar" /&gt;
              &lt;include name="**/resources/**" /&gt;
          &lt;/fileset&gt;
      &lt;/copy&gt;
  &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Manifest file for common. The included class-path is the exact same in all other manifest files</p>
  
  <pre><code>Main-Class: Common.gui.emersonWrapper
  Class-Path: . utils.jar lib/pfcasync.jar lib/poi-3.7/poi-ooxml-schemas-3.7-20101029.jar lib/poi-3.7/poi-3.7-20101029.jar lib/poi-3.7/poi-ooxml-3.7-20101029.jar lib/poi-3.7/ooxml-lib/xmlbeans-2.3.0.jar lib/poi-3.7/ooxml-lib/dom4j-1.6.1.jar lib/xom/xom-1.2.7.jar lib/jgoodies/jgoodies-common-1.3.0.jar lib/jgoodies/jgoodies-binding-2.6.0.jar lib/resources/** Common.jar
  </code></pre>
  
  <p>At run time the jars all exist in the same folder along with a "lib" folder including all referenced library jars. Along with a few images and some xml data. The one difference I noticed in creating the jars was that the batch file did not create an index. Originally in my ant script I had index="true" and when running for example the pressure.jar I would get a
      java.lang.NoClassDefFoundError. 
  I set the index="false" and my these errors go away, however my relative pathed xml files are not found in lib/resources/xml/a.xml
  The real kicker in all of this is the disparity between build methods. I've spent two days going over the jar structure, folder structure, and class-path. In the build.xml I even linked to the manifest files being used in the batch file to no success. Everything comes up the same so why the differences in the way these jars act at run time. I feel like there is a tiny detail that I'm missing. </p>