使用自定义SSLSocketFactory时ClassNotFoundException

时间:2011-11-24 08:19:43

标签: java service classpath

我创建了一个自定义SSLSocketFactory类并将其设置如下

ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl");
ldapEnv.put(FACTORY_SOCKET, socketFactoryClass);

LdapContext ldapContext = new InitialLdapContext(ldapEnv, null);

从Eclipse Dev Environment运行并从命令提示符运行它作为Jar文件时,它工作正常。但是当我将它包装在服务包装器中并将其作为Windows服务启动时,它不起作用。我得到以下异常,

javax.naming.CommunicationException: 192.168.100.22:636 [Root exception is java.lang.ClassNotFoundException: com/testing/diraccess/service/ActiveDirectory$TestSSLFactory]
at com.sun.jndi.ldap.Connection.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapClient.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.ldap.InitialLdapContext.<init>(Unknown Source)

Caused by: java.lang.ClassNotFoundException: com/testing/diraccess/service/ActiveDirectory$TestSSLFactory
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.jndi.ldap.VersionHelper12.loadClass(Unknown Source)
at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
... 35 more

任何帮助???

2 个答案:

答案 0 :(得分:0)

似乎这个问题可能是由于JNDI依赖于设置正确的线程上下文类加载器(管理员可能没有这样做),这是必需的,因为JNDI类是由加载器层次结构和类加载器的类加载器加载的。将找不到应用程序/容器类加载器加载的类。所以JNDI提供了一个钩子来通过线程上下文类加载器来加载这些类。

看看是否有效,

 // Save the current thread context class loader
 ClassLoader currentCL = Thread.currentThread().getContextClassLoader();

// Set the context class loader to the one we know for sure loaded classes inside configstore-core.jar
Thread.currentThread().setContextClassLoader(OmParticipant.class.getClassLoader());

// Invoke the jndi related code that will internally try to load our socket factory impl
...

//restore the original class loader
Thread.currentThread().setContextClassLoader(currentCL );

答案 1 :(得分:0)

我发布此问题已经很长时间了。由于这篇文章没有任何答案,似乎也得到了一些观点,我想我可以分享我最后解决的问题(我已经在几年前的问题评论部分发布了这个)。

我能够通过使用-Xbootclasspath/a:选项将该jar文件包含在引导加载程序类路径中来解决此问题。但无论如何我不喜欢这个解决方案。