Java 错误:需要在环境或系统属性 LDAP 和 JNDI 中指定类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24307517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:25:58  来源:igfitidea点击:

ERROR: Need to specify class name in environment or system property LDAP and JNDI

javaldapjndi

提问by user840718

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

我收到此错误(捕获 2): javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中: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.

我已经寻找了很多解决方案,但我没有收到错误消息。问题出在哪儿?也许是上下文,但我认为代码是完全正确的。

采纳答案by Priyesh

You have to construct the InitialDirContextobject using the envmap you have populated. i.e. use the following code to construct it;

您必须使用已填充的env映射构造InitialDirContext对象。即使用以下代码来构造它;

ctx = new InitialDirContext(env);

ctx = new InitialDirContext(env);

回答by thomson

This should be ctx = new InitialDirContext(env, null);, with env being a hashtable.

这应该是ctx = new InitialDirContext(env, null);, env 是一个哈希表。

回答by Neelam Chahal

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

创建一个文件 jboss-ejb-client.properties 把它放在 ejbModule 的根目录中,并将 ejbModule 文件夹添加到类路径中。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