title: Java to Active Directory LDAP java.net.SocketException: Connection or outbound has closed
body: <p>Good evening,</p>
  <p>I have been facing this error for a couple of days by now, and despite looking for a solution all over the web, I coul'd fix this error.</p>
  <pre><code>import java.util.Hashtable;
  import javax.naming.Context;
  import javax.naming.NamingEnumeration;
  import javax.naming.directory.Attribute;
  import javax.naming.directory.Attributes;
  import javax.naming.directory.BasicAttribute;
  import javax.naming.directory.BasicAttributes;
  import javax.naming.directory.DirContext;
  import javax.naming.directory.InitialDirContext;
  
  public class LDAPtest {
  public static void main(String[] args) {
      
      try {
          
          String keystorePath = &quot;C:/Program Files/Java/jdk-13.0.2/lib/security/cacerts&quot;;
          System.setProperty(&quot;javax.net.ssl.keyStore&quot;, keystorePath);
           
          System.setProperty(&quot;javax.net.ssl.keyStorePassword&quot;, &quot;changeit&quot;);
          
          Hashtable&lt;String, String&gt; ldapEnv = new Hashtable&lt;&gt;(); 
          ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY,&quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
          ldapEnv.put(Context.PROVIDER_URL, &quot;ldaps://localhost:10636&quot;);
          ldapEnv.put(Context.SECURITY_AUTHENTICATION,&quot;simple&quot;);
          ldapEnv.put(Context.SECURITY_PRINCIPAL,&quot;uid=admin,ou=system&quot;);
          ldapEnv.put(Context.SECURITY_CREDENTIALS,&quot;secret&quot;);
          
          DirContext connection = new InitialDirContext(ldapEnv);
          System.out.println(&quot;Benvenuto &quot; + connection);
          NamingEnumeration enm = connection.list(&quot;&quot;);
  
          while (enm.hasMore()) {
              System.out.println(enm.next());
          }
  
          enm.close();
          connection.close();
          
          
      }catch(Exception e) {
          e.printStackTrace();
      }
      
  }
  </code></pre>
  <p>}</p>
  <p>This code is actually working when SSL is not tested, replacing the</p>
  <pre><code>ldapEnv.put(Context.PROVIDER_URL, &quot;ldaps://localhost:10636&quot;);
  </code></pre>
  <p>with</p>
  <pre><code>ldapEnv.put(Context.PROVIDER_URL, &quot;ldap://localhost:10389&quot;);
  </code></pre>
  <p>I made the setup for the LDAP server with Apache Directory Studio, and followed this tutorial here in order to get the LDAPS to work:
  <a href="http://directory.apache.org/apacheds/basic-ug/3.3-enabling-ssl.html" rel="nofollow noreferrer">http://directory.apache.org/apacheds/basic-ug/3.3-enabling-ssl.html</a></p>
  <p>So I made the certificate, even installed it and imported it with keytool into cacerts.
  I enabled portforwarding for the chosen port (10636), but still, I'm getting this exception:</p>
  <pre><code>javax.naming.CommunicationException: simple bind failed: localhost:10636 [Root exception is 
  java.net.SocketException: Connection or outbound has closed]
  at java.naming/com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:219)
  at java.naming/com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2795)
  at java.naming/com.sun.jndi.ldap.LdapCtx.&lt;init&gt;(LdapCtx.java:320)
  at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxFromUrl(LdapCtxFactory.java:225)
  at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:189)
  at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:243)
  at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:154)
  at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:84)
  at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:730)
  at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
  at java.naming/javax.naming.InitialContext.init(InitialContext.java:236)
  at java.naming/javax.naming.InitialContext.&lt;init&gt;(InitialContext.java:208)
  at java.naming/javax.naming.directory.InitialDirContext.&lt;init&gt;(InitialDirContext.java:130)
  at Prova3.main(Prova3.java:31)
  Caused by: java.net.SocketException: Connection or outbound has closed
  at java.base/sun.security.ssl.SSLSocketImpl$AppOutputStream.write(SSLSocketImpl.java:1246)
  at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
  at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
  at java.naming/com.sun.jndi.ldap.Connection.writeRequest(Connection.java:398)
  at java.naming/com.sun.jndi.ldap.Connection.writeRequest(Connection.java:371)
  at java.naming/com.sun.jndi.ldap.LdapClient.ldapBind(LdapClient.java:359)
  at java.naming/com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:214)
  ... 13 more
  </code></pre>
  <p>Thank you in advance</p>