thomas908
asked on
help in setting classpath in ant
i am using windows2000 and have installed ant in D Drive.
My directory structure is something like
D:\ant\source\src\com\pack 1\pack2 (com\pack1\pack2 are packages)
D:\ant\source\conf.jar
D:\ant\source\datasocket.j ar
D:\ant\source\thirdparty\x ercesImpl. 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-b in.jar" />
<pathelement location="${baselib}\photo s.jar" />
<pathelement location="${baselib}\conf. jar" />
<pathelement location="${baselib}\jh.ja r" />
<pathelement location="${baselib}\jhall .jar" />
<pathelement location="${baselib}\jhbas ic.jar" />
<pathelement location="${baselib}\jhsea rch.jar" />
<pathelement location="${baselib}\datas ocket.jar" />
<pathelement location="${baselib}\log4j -1.2.7.jar " />
<pathelement location="${baselib}\third party\xerc esImpl.jar " />
<pathelement location="${baselib}\third party\xmlP arserAPIs. 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\pack 1\pack2\Te st.java
143: cannot resolve symbol
[javac] symbol : class Logger
[javac] location: class com\pack1\pack2\Test.java
[javac] private Logger logger = Logger.getLogger(Test.clas s);
...
My directory structure is something like
D:\ant\source\src\com\pack
D:\ant\source\conf.jar
D:\ant\source\datasocket.j
D:\ant\source\thirdparty\x
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
<pathelement location="${baselib}\photo
<pathelement location="${baselib}\conf.
<pathelement location="${baselib}\jh.ja
<pathelement location="${baselib}\jhall
<pathelement location="${baselib}\jhbas
<pathelement location="${baselib}\jhsea
<pathelement location="${baselib}\datas
<pathelement location="${baselib}\log4j
<pathelement location="${baselib}\third
<pathelement location="${baselib}\third
</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\pack
143: cannot resolve symbol
[javac] symbol : class Logger
[javac] location: class com\pack1\pack2\Test.java
[javac] private Logger logger = Logger.getLogger(Test.clas
...
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
<target name="compile" depends="CopyFiles" >
<javac srcdir="${source}" destdir="${build}">
<classpath>
<pathelement location="{baselib}\mysql- connector- java-3.0.6 -stable-bi n.jar" />
<pathelement location="{baselib}\photos .jar" />
<pathelement location="{baselib}\conf.j ar" />
<pathelement location="{baselib}\jh.jar " />
<pathelement location="{baselib}\jhall. jar" />
<pathelement location="D:\ant\source\jh basic.jar" />
<pathelement location="{baselib}\jhsear ch.jar" />
<pathelement location="D:\ant\source\da tasocket.j ar" />
<pathelement location="D:\ant\source\lo g4j-1.2.7. jar" />
<pathelement location="{baselib}\thirdp arty\xerce sImpl.jar" />
<pathelement location="{baselib}\thirdp arty\xmlPa rserAPIs.j ar" />
</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?
<javac srcdir="${source}" destdir="${build}">
<classpath>
<pathelement location="{baselib}\mysql-
<pathelement location="{baselib}\photos
<pathelement location="{baselib}\conf.j
<pathelement location="{baselib}\jh.jar
<pathelement location="{baselib}\jhall.
<pathelement location="D:\ant\source\jh
<pathelement location="{baselib}\jhsear
<pathelement location="D:\ant\source\da
<pathelement location="D:\ant\source\lo
<pathelement location="{baselib}\thirdp
<pathelement location="{baselib}\thirdp
</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?
ASKER
i am using windows
> 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\pack 1\pack2\Te st.java
>143: cannot resolve symbol
> [javac] symbol : class Logger
> [javac] location: class com\pack1\pack2\Test.java
> [javac] private Logger logger = Logger.getLogger(Test.clas s);
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.
> are not found, Why is it so?
It should work with absolute paths as well.
>[javac] D:\ant\source\src\com\pack
>143: cannot resolve symbol
> [javac] symbol : class Logger
> [javac] location: class com\pack1\pack2\Test.java
> [javac] private Logger logger = Logger.getLogger(Test.clas
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.
ASKER
Is <pathelement location="{baselib}\jhall. jar" />
correct way of specifying classapth
correct way of specifying classapth
Yes this is correct given that there is a "basedir" variabel defined somewhere.
ASKER
What i want to know is in some pathelements in classpath i am specifying the classpath as
<pathelement location="D:\ant\source\lo g4j-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-bi n.jar" />
<pathelement location="{baselib}\photos .jar" />
<pathelement location="{baselib}\conf.j ar" />
<pathelement location="{baselib}\jh.jar " />
<pathelement location="{baselib}\jhall. jar" />
<pathelement location="D:\ant\source\jh basic.jar" />
<pathelement location="{baselib}\jhsear ch.jar" />
<pathelement location="D:\ant\source\da tasocket.j ar" />
<pathelement location="D:\ant\source\lo g4j-1.2.7. jar" />
<pathelement location="{baselib}\thirdp arty\xerce sImpl.jar" />
<pathelement location="{baselib}\thirdp arty\xmlPa rserAPIs.j ar" />
</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\jh basic.jar" /> which i want to avoid. If i write here <pathelement location="{baselib}\jhbasi c.jar" /> it doesn't work
<pathelement location="D:\ant\source\lo
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
is a better option. But in my case specfying classpath as <pathelement location="{baselib}\photos
does not work at some places.
classpath>
<pathelement location="{baselib}\mysql-
<pathelement location="{baselib}\photos
<pathelement location="{baselib}\conf.j
<pathelement location="{baselib}\jh.jar
<pathelement location="{baselib}\jhall.
<pathelement location="D:\ant\source\jh
<pathelement location="{baselib}\jhsear
<pathelement location="D:\ant\source\da
<pathelement location="D:\ant\source\lo
<pathelement location="{baselib}\thirdp
<pathelement location="{baselib}\thirdp
</classpath>
First 4 pathelemets are specified as <pathelement location="{baselib}\photos
ASKER
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\s rc" />
<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-bi n.jar" />
<pathelement location="{baselib}\photos .jar" />
<pathelement location="{baselib}\conf.j ar" />
<pathelement location="{baselib}\jh.jar " />
<pathelement location="{baselib}\jhall. jar" />
<pathelement location="D:\ant\source\jh basic.jar" />
<pathelement location="{baselib}\jhsear ch.jar" />
<pathelement location="D:\ant\source\da tasocket.j ar" />
<pathelement location="D:\ant\source\lo g4j-1.2.7. jar" />
<pathelement location="{baselib}\thirdp arty\xerce sImpl.jar" />
<pathelement location="{baselib}\thirdp arty\xmlPa rserAPIs.j ar" />
</classpath>
</javac>
</target>
</project>
<?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\s
<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-
<pathelement location="{baselib}\photos
<pathelement location="{baselib}\conf.j
<pathelement location="{baselib}\jh.jar
<pathelement location="{baselib}\jhall.
<pathelement location="D:\ant\source\jh
<pathelement location="{baselib}\jhsear
<pathelement location="D:\ant\source\da
<pathelement location="D:\ant\source\lo
<pathelement location="{baselib}\thirdp
<pathelement location="{baselib}\thirdp
</classpath>
</javac>
</target>
</project>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
<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
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 (https://www.experts-exchange.com/Community_Support/) for zero points to PAQ the question and Refund the points back to you.
ASKER
Thank you so much for helping
Thank you for accepting, glad we were of help :)
, 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