SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Ant Classpath Problem

    Hi, I'm using Eclipse and Ant to create a Spring web application project which builds and deploys fine on its own, but when I add another project (a jar library) and call methods in the jar I get build errors from Ant. Does anyone know what I'm doing wrong?

    I've dropped the TestLibrary.jar into SpringSkeleton/WEB-INF/lib but the following compiler errors appear:

    Code:
    Buildfile: /Users/russell/Projects/Eclipse/SpringSkeleton/build.xml
    Compile:
        [javac] Compiling 1 source file to /Users/russell/Projects/Eclipse/SpringSkeleton/WEB-INF/classes
        [javac] /Users/russell/Projects/Eclipse/SpringSkeleton/src/springskeleton/controllers/ServerTimeController.java:10: 
    package testlibrary does not exist
        [javac] import testlibrary.Foo;
        [javac]                    ^
        [javac] /Users/russell/Projects/Eclipse/SpringSkeleton/src/springskeleton/controllers/ServerTimeController.java:19: 
    cannot find symbol
        [javac] symbol  : variable Foo
        [javac] location: class springskeleton.controllers.ServerTimeController
        [javac] 		String s = Foo.f();
        [javac]                            ^
        [javac] 2 errors
    
    BUILD FAILED
    ----

    build.xml for TestLibrary
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <project name="TestLibrary" basedir="." default="Build">
    	<property name="src.dir" value="src"/>
    	<property name="build.dir" value="bin"/>
    
    
    	<target name="Build" depends="Compile">
    		<jar destfile="TestLibrary.jar">
    			<fileset dir="." includes="**/*.class"/>
    		</jar>
    	</target>
    
    	<target name="Compile">
    		<mkdir dir="${build.dir}"/>
    		<javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
    			<src path="${src.dir}"/>
    			<classpath refid="build.classpath"/>
    		</javac>
    	</target>
    
    	<target name="Clean" description="Clean output directories">
    		<delete>
    			<fileset dir="${build.dir}">
    				<include name="**/*.class"/>
    			</fileset>
    		</delete>
    	</target>
    </project>
    ----

    build.xml for SpringSkeleton
    Code:
    <project name="SpringSkeleton" basedir="." default="Build">
    	<property file="build.properties"/>
    	<property name="src.dir" value="src"/>
    	<property name="build.dir" value="WEB-INF/classes"/>
    
    	<path id="build.classpath">
    		<fileset dir="WEB-INF/lib">
    			<include name="*.jar"/>
    		</fileset>
    		<fileset dir="${appserver.lib}">
    			<include name="*servlet*.jar"/>
    		</fileset>
    		<pathelement path="${build.dir}"/>
    	</path>
    
    	<target name="Build" depends="Compile">
    		<jar destfile="springskeleton.war">
    			<fileset dir="." includes="WEB-INF/**/*, jsp/**/*"/>
    		</jar>
    	</target>
    
    	<target name="Compile">
    		<mkdir dir="${build.dir}"/>
    		<javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
    			<src path="${src.dir}"/>
    			<classpath refid="build.classpath"/>
    		</javac>
    	</target>
    
    	<target name="Deploy">
    		<copy file="springskeleton.war" todir="${appserver.deploy}"/>
    	</target>
    
    	<target name="Clean" description="Clean output directories">
    		<delete>
    			<fileset dir="${build.dir}">
    				<include name="**/*.class"/>
    			</fileset>
    		</delete>
    	</target>
    </project>
    ----

    TestLibrary > src > testlibrary > Foo.java
    Code:
    package testlibrary;
    
    public class Foo {
    	public static String f() {
    		return "Test";
    	}
    }
    ----

    ServerTimeController.java
    Code:
    import <other_packages>;
    import testlibrary.Foo;
    
    
    public class ServerTimeController implements Controller {
    	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) 
    	throws ServletException, IOException {
    		ModelAndView modelAndView = new ModelAndView("servertime");
    		modelAndView.addObject("serverTime", new Date().toString());
    
    		String s = Foo.f();
    		
    		return modelAndView;
    	}
    }
    Thanks!
    Last edited by russell42; Nov 27, 2008 at 05:10.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the location of Foo.java seems to be wrong.
    It should be in src/testlibrary/FOO.java

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, I'm afraid that was a typo in my original post though. It is TestLibrary/src/testlibrary/Foo.java.

    The TestLibrary.jar compiles fine, I extracted it and Foo.class exists. It seems to just be the build.xml file for SpringSkeleton that isn't picking it up, even though I set the classpath:

    <path id="build.classpath">
    <fileset dir="WEB-INF/lib">
    <include name="*.jar"/>
    </fileset>
    <fileset dir="${appserver.lib}">
    <include name="*servlet*.jar"/>
    </fileset>
    <pathelement path="${build.dir}"/>
    </path>

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try replacing
    <include name="*.jar"/>
    with
    <include name="**/*.jar"/>

  5. #5
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately that produces the same error, it still isn't picking up the .jar file. Could it be something to do with my TestLibrary project? I have Spring Framework .jar files in WEB-INF/lib and they are included fine.

    Thanks again!

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can u send me your TestLibrary.jar file?

    and how are you generating TestLibrary.jar file?

  7. #7
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for your help, it is very much appreciated. I'm heading in to work now but I'll upload the entire project later on. :-)

  8. #8
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by russell42 View Post
    Thanks for the reply, I'm afraid that was a typo in my original post though. It is TestLibrary/src/testlibrary/Foo.java.

    The TestLibrary.jar compiles fine, I extracted it and Foo.class exists. It seems to just be the build.xml file for SpringSkeleton that isn't picking it up, even though I set the classpath:

    <path id="build.classpath">
    <fileset dir="WEB-INF/lib">
    <include name="*.jar"/>
    </fileset>
    <fileset dir="${appserver.lib}">
    <include name="*servlet*.jar"/>
    </fileset>
    <pathelement path="${build.dir}"/>
    </path>
    You're using a relative file path, but relative to what?

    When you run a build.xml, relative paths are relative to the 'running' location of Ant, which is not, I guarantee you, the same place that you have you build.xml stored.

    As such, I resorted to absolute paths, and some validation, for my Ant builds, as so:
    Code:
      <!-- Libraries required for the project -->
      <property name="struts.jar" location="c:/_jars/struts-1.2.9.jar"/>
    
      <!-- Create the classpath for the project -->
      <path id="project.classpath">
        <pathelement location="${struts.jar}"/>
      </path>
    
      <!-- Initialize the project -->
      <target name="init">
        <!-- Verify all of the jars are available -->
        <available file="${struts.jar}" property="struts.jar.ok"/>
        
        <fail unless="struts.jar.ok">${struts.jar} is missing, misspelled, or has a bad path</fail>
      </target>
      
      <!-- Compile the java code from ${javac.src} -->
      <target name="compile" depends="init">
    
        <mkdir dir="${javac.build}"/>
    
        <javac srcdir      = "${javac.src}"
               destdir     = "${javac.build}"
               source      = "${javac.source}"
               debug       = "${javac.debug}"
               debuglevel  = "${javac.debuglevel}"
               optimize    = "${javac.optimize}"
               deprecation = "${javac.deprecation}"
               target      = "${javac.target.version}" >
    
          <classpath refid="project.classpath" />
    
        </javac>
        
      </target>
    Using Ant this way allows me to have one place on my disk to store all the jars I have, keep them somewhat organized (ok, not really, it's just a giant jar pool) and up to date.

  9. #9
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi rushiku, thanks for the reply. I'm going to post my entire project once I get home from work but I don't think relative paths could be causing it since my Spring .jars (commons-logging.jar etc.) are being included properly and they're in WEB-INF/lib too, I'll try your approach though.

  10. #10
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Fair enough, though I'm thinking that the jars that are working have copies somewhere that's defined on your system's classpath env variable.

  11. #11
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    I've uploaded the code to http://www.ftssolutions.com/code.zip if anyone would be kind enough to take a look, thanks.

  12. #12
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is with your TestLibrary.jar file
    The location of Foo.class is "TestLibrary.jar/bin/testlibrary/Foo.class" it is wrong

    It should be "TestLibrary.jar/testlibrary/Foo.class"

  13. #13
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i repackged the TestLibrary.jar and it worked

  14. #14
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much! I put . in the build.dir property instead of bin and it worked perfectly, thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •