NoInitialContextException

Lograine

Mitglied
Hallöchen,

ich soll ein kleines Firmen internes Telefon ,in Java geschrieben, um eine Suchfunktion erweitern, die die Suche auf einem LDAP Server realisiert.Nun bekomme ich bei meiner Query eine Exception javax.naming.NoInitialContextException.
Ergooglet hab ich mir , dass das an einer fehlenden Properties Datei liegt.
Jetzt hab ich allerdings keine Ahnung was für properties da nun drin erwartet werden- nur der LDAP server also so in der Art: java.naming.provider.url=unser ldap server
geht schon mal nicht.
Hat da jemand vielleicht eine Idee zu oder einen Vorschlag?
 

FArt

Top Contributor
Bei der URL fehlt vermutlich noch das Protokoll. Später wirst du wohl noch darauf stoßen, dass noch mehr fehlt.

Hier fehlen zumindest Infos über den Server und evtl. etwas minimaler Beispiecode, z.B. ein einfacher Zugriff.

Das hier war einfach zu googeln (InitialContext ldap): The Initial Context
 

Lograine

Mitglied
auf der Seite bin ich schon so gut wie zuhause- aber das birngt mich nicht wirklich weiter
ja okay mein Code den ich hab :

private LdapContext myLdapConn;
private Properties myPrefs;
sind globale Variable

die Methode macht mir eine Verbindung auf, wenn ich sie alleine laufen lasse bekomme ich zumindest keine exceptions also gehe ich davon aus , dass sie funktioniert
Java:
    public static void initLdapConnection() {
        Properties env = new Properties();
        env.put("java.naming.ldap.attributes.binary", "userCertificate");
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://"
                + myPrefs.getProperty("ldapServerName"));
        env.put(Context.SECURITY_AUTHENTICATION, "none");
     try {
            myLdapConn = new InitialLdapContext(env, null);
        } catch (NamingException ex) {
            System.out.println("Naming Exception");
        }
    }

dann habe ich eine Methode die die Atribute einliest bzw ausgeben soll

Java:
   public void printAttrs(Attributes attrs) {
        if (attrs == null) {
          System.out.println("No attributes");
        } else {
          /* Print each attribute */
          try {
            for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
              Attribute attr = (Attribute) ae.next();
              System.out.println("attribute: " + attr.getID());

              /* print each value */
              for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out
                  .println("value: " + e.next()))
                ;
            }
          } catch (NamingException e) {
            e.printStackTrace();}}
          }

und zu guter letzt die suche:

[Java]
public void serachLdap() throws NamingException{
Hashtable env = new Hashtable();

DirContext ctx = new InitialDirContext(env);

SearchControls sc = new SearchControls();
String[] attrIDs = { "sn", "cn", "o", "mail" };
Attributes answer = ctx
.getAttributes("cn=Wenzel Kalus, ou=People", attrIDs);
printAttrs(answer);
}
[/code]

und diesen Kalus Wenzel gibt es auf dem server auch

-> javax.naming.NoInitialContextException: Need to specify class name in environment or system property
 
Zuletzt bearbeitet:

Neue Themen


Oben