ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Darin_Haw...@cargill.com
Subject classpathref weirdness
Date Wed, 02 May 2001 13:25:41 GMT

I'm having a problem using a classpathref for both compilation and 
execution. It appears to work properly for the former but not the 
later, 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>
 /classes
 /lib
  jndi.jar         # JNDI 1.2.1
  ldap.jar         # LDAP SP 1.2.2
  providerutil.jar # LDAP SP 1.2.2
 /src
  Test.java # attempts to get an InitialDirContext
 build.bat # sets ANT_HOME, JAVA_HOME (1.2), clears CLASSPATH, starts 
ant
 build.xml # defines path to include jars and classes dir, compiles, 
runs

Note: The use of JDK 1.2 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.

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. 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 -----


Mime
View raw message