Solved

help in setting classpath in ant

Posted on 2004-08-14
15
757 Views
Last Modified: 2013-11-23
i am using windows2000 and have installed ant in D Drive.
My directory structure is something like

D:\ant\source\src\com\pack1\pack2 (com\pack1\pack2 are packages)
D:\ant\source\conf.jar
D:\ant\source\datasocket.jar
D:\ant\source\thirdparty\xercesImpl.jar

and I have created the follwoing build.xml

<?xml version="1.0"?>

<project name="Proj" default="compile" basedir=".">
     <property name="name" value="Proj" />
     <property name="build" value="${basedir}\build" />
     <property name="source" value="${basedir}\source" />
     <property name="baselib" value="\source" />

        <target name="compile">
        <javac srcdir="${source}" destdir="${build}">
           <classpath>

              <pathelement location="${baselib}\mysql-connector-java-3.0.6-stable-bin.jar" />
              <pathelement location="${baselib}\photos.jar" />
              <pathelement location="${baselib}\conf.jar" />
              <pathelement location="${baselib}\jh.jar" />
              <pathelement location="${baselib}\jhall.jar" />
              <pathelement location="${baselib}\jhbasic.jar" />
              <pathelement location="${baselib}\jhsearch.jar" />
              <pathelement location="${baselib}\datasocket.jar" />
              <pathelement location="${baselib}\log4j-1.2.7.jar" />
              <pathelement location="${baselib}\thirdparty\xercesImpl.jar" />
              <pathelement location="${baselib}\thirdparty\xmlParserAPIs.jar" />


           </classpath>
       </javac>
    </target>

</project>

But when i try to execute it, i get errors  which indicate that java files cannot find the jar files that is some problem with the classpath.

   [javac] D:\ant\source\src\com\pack1\pack2\Test.java
143: cannot resolve symbol
   [javac] symbol  : class Logger
   [javac] location: class com\pack1\pack2\Test.java
   [javac]     private Logger logger = Logger.getLogger(Test.class);
...
0
Comment
Question by:thomas908
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
15 Comments
 
LVL 9

Expert Comment

by:ronan_40060
ID: 11803385
Hello
 , MyComputer -> Properties -> Environment and then define a new variable CLASSPATH and give it some value.
 also by adding the jar file to your 'ext' directory of your jdk and jre.
let me know how it goes
ronan
0
 
LVL 8

Author Comment

by:thomas908
ID: 11803397
Thanks for replying.
Is it possible to do it from build.xml?
I don't want to set classapth in environment variables. Basically I want to give the user the option to compile the java files. It won't be very appropriate if i ask her to set the classpath in environment variables before compiling.
0
 
LVL 9

Assisted Solution

by:ronan_40060
ronan_40060 earned 250 total points
ID: 11803461
This is with reference with build.xml examples in jaxb  
<property name="jwsdp.home" value="c:/jwsdp-1.3"/> <!-- customize this -->

    <path id="classpath">
        <pathelement path="."/>
        <!-- for use with bundled ant -->
        <fileset dir="${jwsdp.home}" includes="jaxb/lib/*.jar"/>
         <fileset dir="${jwsdp.home}" includes="jwsdp-shared/lib/*.jar"/>
       <fileset dir="${jwsdp.home}" includes="jaxp/lib/**/*.jar"/>
       
    </path>

    <taskdef classname="com.sun.tools.xjc.XJCTask" name="xjc">
        <classpath refid="classpath"/>
    </taskdef>

    <target name="compile">

        <javac debug="on" destdir="." srcdir=".">
            <classpath refid="classpath"/>
            <include name="BaseClass.java"/>
         <exclude name="Main.java"/>
        </javac>

        <!-- generate the Java content classes from the schema -->
        <echo message="Compiling the schema..."/>
        <xjc binding="binding.jxb" extension="true" package="xxx" schema="po.xsd" target="."/>
        <!-- generate the javadocs from the content classes -->
        <mkdir dir="docs/api"/>
        <javadoc destdir="docs/api" packagenames="xxx" sourcepath="." windowtitle="Generated Interfaces for po.xsd">
            <classpath refid="classpath"/>
         </javadoc>

        <!-- compile all of the java sources -->
        <echo message="Compiling the java source files..."/>
        <javac debug="on" destdir="." srcdir=".">
            <classpath refid="classpath"/>
        </javac>
    </target>
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 8

Author Comment

by:thomas908
ID: 11808365
<target name="compile" depends="CopyFiles" >
    <javac srcdir="${source}" destdir="${build}">
      <classpath>
              <pathelement location="{baselib}\mysql-connector-java-3.0.6-stable-bin.jar" />
              <pathelement location="{baselib}\photos.jar" />
              <pathelement location="{baselib}\conf.jar" />
              <pathelement location="{baselib}\jh.jar" />
              <pathelement location="{baselib}\jhall.jar" />
              <pathelement location="D:\ant\source\jhbasic.jar" />
              <pathelement location="{baselib}\jhsearch.jar" />
              <pathelement location="D:\ant\source\datasocket.jar" />
              <pathelement location="D:\ant\source\log4j-1.2.7.jar" />
              <pathelement location="{baselib}\thirdparty\xercesImpl.jar" />
              <pathelement location="{baselib}\thirdparty\xmlParserAPIs.jar" />
     </classpath>
   </javac>
</target>

For some I have specified relative paths. For I have to specify absolute paths, otherwise they are not found, Why is it so?
0
 
LVL 8

Author Comment

by:thomas908
ID: 11808368
i am using windows
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808402
> For some I have specified relative paths. For I have to specify absolute paths, otherwise they
> are not found, Why is it so?

It should work with absolute paths as well.

>[javac] D:\ant\source\src\com\pack1\pack2\Test.java
>143: cannot resolve symbol
>   [javac] symbol  : class Logger
>   [javac] location: class com\pack1\pack2\Test.java
>   [javac]     private Logger logger = Logger.getLogger(Test.class);

Is the Logger class in the log4j-1.2.7.jar file? It might be missing from another jar file that you do not include in your classpath.
0
 
LVL 8

Author Comment

by:thomas908
ID: 11808549
Is <pathelement location="{baselib}\jhall.jar" />
correct way of specifying classapth
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808630
Yes this is correct given that there is a "basedir" variabel defined somewhere.
0
 
LVL 8

Author Comment

by:thomas908
ID: 11808732
What i want to know is in some pathelements in classpath i am specifying the classpath as

 <pathelement location="D:\ant\source\log4j-1.2.7.jar" />

I don't think that is the right way of doing because user may create directories with differnt names. So specyfing the classpath like

 <pathelement location="{baselib}\photos.jar" />

is a better option. But in my case specfying classpath as  <pathelement location="{baselib}\photos.jar" />
does not work at some places.


classpath>
              <pathelement location="{baselib}\mysql-connector-java-3.0.6-stable-bin.jar" />
              <pathelement location="{baselib}\photos.jar" />
              <pathelement location="{baselib}\conf.jar" />
              <pathelement location="{baselib}\jh.jar" />
              <pathelement location="{baselib}\jhall.jar" />
              <pathelement location="D:\ant\source\jhbasic.jar" />
              <pathelement location="{baselib}\jhsearch.jar" />
              <pathelement location="D:\ant\source\datasocket.jar" />
              <pathelement location="D:\ant\source\log4j-1.2.7.jar" />
              <pathelement location="{baselib}\thirdparty\xercesImpl.jar" />
              <pathelement location="{baselib}\thirdparty\xmlParserAPIs.jar" />
     </classpath>

First 4 pathelemets are specified as  <pathelement location="{baselib}\photos.jar" /> whereas the 5th pathelement is specified by using complete path     <pathelement location="D:\ant\source\jhbasic.jar" /> which i want to avoid. If i write here     <pathelement location="{baselib}\jhbasic.jar" /> it doesn't work
0
 
LVL 8

Author Comment

by:thomas908
ID: 11808739
Here is complete build.xml

<?xml version="1.0"?>

<project name="M" default="compile" basedir=".">
<property name="name" value="M" />
<property name="build" value="${basedir}\build" />
<property name="source" value="${basedir}\source\src" />
<property name="baselib" value="\source" />

<target name="init" >

     <mkdir dir="build" />
     <mkdir dir="build\classes" />
</target>

<target name="ClassesCopyFiles" depends="init">
    <copy toDir="build\classes">
      <fileset dir="source\src"
        includes="**/*.*">
        <exclude name="**/*.java"/>
      </fileset>
    </copy>
</target>

<target name="CopyFiles" depends="ClassesCopyFiles">
    <copy toDir="build">
      <fileset dir="source"
        includes="**/*.*">
        <exclude name="**/*.java"/>
          <exclude name="src/**"/>
      </fileset>
    </copy>
</target>



<target name="compile" depends="CopyFiles" >
    <javac srcdir="${source}" destdir="${build}\classes">
      <classpath>
              <pathelement location="{baselib}\mysql-connector-java-3.0.6-stable-bin.jar" />
              <pathelement location="{baselib}\photos.jar" />
              <pathelement location="{baselib}\conf.jar" />
              <pathelement location="{baselib}\jh.jar" />
              <pathelement location="{baselib}\jhall.jar" />
              <pathelement location="D:\ant\source\jhbasic.jar" />
              <pathelement location="{baselib}\jhsearch.jar" />
              <pathelement location="D:\ant\source\datasocket.jar" />
              <pathelement location="D:\ant\source\log4j-1.2.7.jar" />
              <pathelement location="{baselib}\thirdparty\xercesImpl.jar" />
              <pathelement location="{baselib}\thirdparty\xmlParserAPIs.jar" />
     </classpath>
   </javac>
</target>



</project>
0
 
LVL 35

Accepted Solution

by:
girionis earned 250 total points
ID: 11808829
> What i want to know is in some pathelements in classpath i am specifying the classpath as
>
> <pathelement location="D:\ant\source\log4j-1.2.7.jar" />
>
> I don't think that is the right way of doing because user may create directories with differnt names.

Yes you are right, it is not the proper way of doing it. What I suggest you do is to bundle everything up in a zip file and then use relative paths from there. By doing so you have control over which folder all files are unzipped and you can follow the paths from there. For exampel you can uninstall everythign into myfolder/jarFiles so you have a structure

myfolder--
        jarFiles--
                xercesImple.jar
                otherjar.jar

and so on, so since you will have everything inside the myfolder directory you can then do:

<pathelement location="./jarFiles/xercesImpl.jar" />

and so on..

And, just to try something else, what happens if you move your log4j jar file where you have the rest and try :

<target name="compile" depends="CopyFiles" >
    <javac srcdir="${source}" destdir="${build}\classes">
     <classpath>
              <pathelement location="{baselib}\mysql-connector-java-3.0.6-stable-bin.jar" />
              <pathelement location="{baselib}\photos.jar" />
              <pathelement location="{baselib}\conf.jar" />
              <pathelement location="{baselib}\jh.jar" />
              <pathelement location="{baselib}\jhall.jar" />
              <pathelement location="D:\ant\source\jhbasic.jar" />
              <pathelement location="{baselib}\jhsearch.jar" />
              <pathelement location="D:\ant\source\datasocket.jar" />
              <pathelement location="{baselib}\thirdparty\log4j-1.2.7.jar" />
              <pathelement location="{baselib}\thirdparty\xercesImpl.jar" />
              <pathelement location="{baselib}\thirdparty\xmlParserAPIs.jar" />
     </classpath>
   </javac>
</target>

are you still getting the same error message?
0
 
LVL 8

Author Comment

by:thomas908
ID: 11809922
this solved the problem. Don't know why pathelement didn't work

<path id="My.classpath" >
    <fileset dir="source">
      <include name="**/*.jar" />
    </fileset>
    <pathelement location="source" />
  </path>


<target name="compile" depends="CopyFiles" >
    <javac srcdir="${source}" destdir="${build}\classes">
        <classpath refid="My.classpath"/>
   </javac>
</target>


thanks
0
 
LVL 35

Expert Comment

by:girionis
ID: 11809965
Nice to hear problem is solved. If you are happy with the comments please accept an answer otherwise please ask a question to the Community Support forum (http://www.experts-exchange.com/Community_Support/) for zero points to PAQ the question and Refund the points back to you.
0
 
LVL 8

Author Comment

by:thomas908
ID: 11817479
Thank you so much for helping
0
 
LVL 35

Expert Comment

by:girionis
ID: 11817890
Thank you for accepting, glad we were of help :)
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question