ERROR: Need to specify class name in environment or system property LDAP and JNDI
Solution 1
You have to construct the InitialDirContext object using the env map you have populated. i.e. use the following code to construct it;
ctx = new InitialDirContext(env);
Solution 2
For JBOSS AS
create initialContext
public static final String PKG_INTERFACE="org.jboss.ejb.client.naming";
public static Context initialContext;
public static Context getInitialContext() throws NamingException{
if(initialContext==null){
Properties properties=new Properties();
properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACE);
initialContext=new InitialContext(properties);
}
public static final String PKG_INTERFACE="org.jboss.ejb.client.naming";
check you are passing correct Sring i.e. "org.jboss.ejb.client.naming" in PKG_INTERFACE .
create a file jboss-ejb-client.properties put it in the root of ejbModule and add ejbModule folder to classpath. contents for jboss-ejb-client.properties
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
Solution 3
This should be ctx = new InitialDirContext(env, null);
, with env being a hashtable.
user840718
Updated on July 09, 2022Comments
-
user840718 almost 2 years
This is my code:
public class TestConnection { public static void main(String[] args) { String password = "s3cret"; Map<String, String> env = new HashMap<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=userdev,dc=local"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); //env.put(Context.SECURITY_PRINCIPAL, "uid="+ username +"cn=users"); // replace with user DN env.put(Context.SECURITY_PRINCIPAL, "cn=dcmanager,cn=users,dc=userdev,dc=local"); // replace with user DN env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = null; try { ctx = new InitialDirContext(); } catch (NamingException e) { // handle } try { SearchControls controls = new SearchControls(); controls.setSearchScope( SearchControls.SUBTREE_SCOPE); ctx.search( "", "(objectclass=person)", controls); // no need to process the results } catch (NameNotFoundException e) { e.printStackTrace(); System.out.println("catch 1"); // The base context was not found. // Just clean up and exit. } catch (NamingException e) { System.out.println("catch 2"); e.printStackTrace(); // exception handling } finally { // close ctx or do Java 7 try-with-resources http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html } } }
I got this error (catch 2) : javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
I have looked for a lot of solutions, but I don't get the error. Where is the problem? Maybe the context, but I think that the code is perfectly correct.
-
user840718 almost 10 yearsIf I do so, I got the following error about it: The constructor InitialDirContext(Map<String,String>) is undefined
-
Priyesh almost 10 yearsSorry, you will have to use a Hashtable instead of a HashMap. docs.oracle.com/javase/7/docs/api/javax/naming/directory/…
-
user840718 almost 10 yearsI almost solved the problem, now I got "NullPointerException". Can be a problem due a bad typing of the wanted resources?
-
Priyesh almost 10 yearsWhere do you get the NullPointerException?
-
user840718 almost 10 yearsSorry, the error is in (catch 2): javax.naming.directory.InvalidSearchFilterException: Empty filter; remaining name '' Because I have empty names. Thank you very much for helping.