Why don't I have this class when it is suppose

2019-05-11 18:09发布

So, I am trying to implement an LDAP connection in java...this requires the com.sun.jndi.ldap.LdapCtxFactory. Jarfinder shows that the LdapCtxFactory should be included in rt.jar, which to my understanding is the base of the java SDK. Eclipse can't find it (I'm trying to import it).

I was having an issue similar to this earlier, but I resolved it by installing the jar which contains it........now I'm super confused, since the jar it is contained in is the default jar?

Note this also is a part of an Android Project. Would that make a difference?

Edit

Where I'm using this package is the following:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapHost);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user + "@" + domain);
env.put(Context.SECURITY_CREDENTIALS, pass);

And then later on I create the Context using the hash table:

ctxGC = new InitialLdapContext(env, null);

So, since I don't have the com.sun, I can't provide that in the hash table. Do I even need that first line? I'm not entirely sure what it is for...

标签: java jar ldap
4条回答
Bombasti
2楼-- · 2019-05-11 18:34

Eclipse hides implementation-specific classes that aren't part of the core Java API with its concept of "access rules". You can override this, but it's dangerous to do so. For example, I suspect very strongly that the Sun implementation is not going to be available on an Android device.

Instead, just create an initial context of the right type, and let the framework find the right implementation to back it up:

LdapContext ctx = new InitialLdapContext();
try {
  /* Use the context... */
} finally {
  ctx.close();
}
查看更多
贼婆χ
3楼-- · 2019-05-11 18:36

Android does not include the Web Services API from the JRE (i.e. javax.naming)

However, all the comments were very useful and informative, and are what eventually led me to find the core of the problem.

I needed to download another library, the UnboundId library.

http://www.unboundid.com/products/ldapsdk/

查看更多
迷人小祖宗
4楼-- · 2019-05-11 18:36

The com.sun classes are part of Sun's implementation and not part of the supported java libraries. You should not rely on them being part of all distros and should not rely on them existing from version to version.

http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

The sun.* packages are not part of the supported, public interface. A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms.

查看更多
Anthone
5楼-- · 2019-05-11 18:43

Packages starting with "com.sun" are normally specific to a Java runtime implementation from Sun. These could be internal classes used by the implementation, or default implementations of APIs. Since different implementations of Java SE exist, which may have different internal package names and classes, using any com.sun.* class is very unsafe since it harms portability amongst runtimes.

Find something in java.* or javax.* that does what you need. Probably a factory pattern that finds an implementation at runtime.

EDIT: in response to your comment in the other answer... Create an InitialLdapContext using the constructor that's suitable for your purposes, then call a newInstance method on it. This will provide you with an LdapContext instance that you can use. The latter is only an inteface, so the exact implementation is hidden. That's service interface providers at work for ya.

查看更多
登录 后发表回答