I originally posted this to ant-user and got no response. Perhaps it's
more of an ant-dev question, so here goes...
I'm having a problem using a classpathref for both compilation and
execution. It appears to work properly for the former but not the
latter, even though it's the exact same refid. I'm hoping this is due
to a bug in Ant, and not just my own ignorance. Here's the
particulars...
I have a simple directory setup...
/<project>
build.bat # sets ANT_HOME, JAVA_HOME (1.2), CLASSPATH, starts ant
build.xml # defines paths, compilation and run targets
/classes
/lib
jndi.jar # JNDI 1.2.1 (java.sun.com/products/jndi)
ldap.jar # LDAP SP 1.2.2
providerutil.jar # LDAP SP 1.2.2
/src
Test.java # attempts to get an InitialDirContext
Test.java refers to classes both statically and dynamically in the jar
files. The compilation executes without fail, but the execution of
Test.java fails with the following...
C:\projects\AntBug\build.xml:27:
javax.naming.NoInitialContextException: Cannot instantiate class:
com.sun.jndi.ldap.LdapCtxFactory [Root exception is
java.lang.ClassNotFoundException: com.sun.jndi.ldap.LdapCtxFactory]
...even though the class does in fact exist in ldap.jar.
Note: This problem isn't specific to JNDI, as I observed similar
behavior in a more trivial example. The use of JDK 1.2 here is
significant, as it does not include JNDI/LDAP support natively. When
using JDK 1.3, this problem does not occur. Also, unless you edit the
Test.java file to refer to a valid LDAP server, success would actually
look like an host not found error.
Here are listings of the specific files...
----- begin build.bat -----
@echo off
set JAVA_HOME=C:\Programs\jdk1.2.2
set ANT_HOME=C:\Programs\jakarta-ant-1.3
set PATH=%PATH%;%ANT_HOME%\bin
set CLASSPATH=
ant -buildfile build.xml %1
----- end build.bat -----
----- begin build.xml -----
<project name="antbug" default="test" basedir=".">
<property name="src" value="src"/>
<property name="classes" value="classes"/>
<path id="lib.class.path">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<path id="classes.class.path">
<pathelement location="classes"/>
</path>
<path id="project.class.path">
<path refid="lib.class.path"/>
<path refid="classes.class.path"/>
</path>
<target name="compile">
<mkdir dir="${classes}"/>
<javac classpathref="project.class.path"
srcdir="${src}" destdir="${classes}"/>
</target>
<target name="test" depends="compile">
<java classpathref="project.class.path" classname="Test"/>
</target>
<target name="clean">
<delete dir="${classes}"/>
</target>
</project>
----- end build.xml -----
----- begin Test.java -----
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
public class Test {
public static void main(String[] args) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,
"ldap://your.directory.com");
DirContext dirCtx = new InitialDirContext(env);
System.out.println("Success.");
}
}
----- end Test.java -----