title: Java SSL context not initialised
body: <p>Hello i'm trying to run a SSL server and client program. I'm first creating a certificate with the cmd command &quot;keytool -genkey -keystore mySrvKeyStore -keyalg RSA&quot;. 123456 is the password after i fill in the info. I put the certificate in the same folder as the server and client, i run the server and I get this error:
  &quot;java.lang.IllegalStateException: SSLContext is not initialized&quot;</p>
  <p>The server:</p>
  <pre><code>public class SSLServer {
  
  private static int port = 4000;
  private static SSLServerSocketFactory sf;
  private static SSLServerSocket ss;
  
  public static void StabilireConexiune(int nrPort) {
      try {
          sf = (SSLServerSocketFactory) SSLServer.getServerSocketFactory();
          ss = (SSLServerSocket) sf.createServerSocket(nrPort);
          System.out.println(&quot;Server connected ready to accept new connections at the address &quot; + ss.getLocalPort());
          String[] enable = {&quot;TLS_DH_anon_WITH_AES_128_CBC_SHA&quot;};
          ss.setEnabledCipherSuites(enable);
          String[] cipherSuites = ss.getEnabledCipherSuites();
          System.out.println(&quot;CipherSuites: &quot;);
          for (int i = 0; i &lt; cipherSuites.length; i++) {
              System.out.println(cipherSuites[i]);
          }
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  
  private static SSLSocket clientSocket;
  
  public static void ConectareClient(){
      
      try{
          clientSocket = (SSLSocket) ss.accept();
          System.out.println(&quot;Client connected succesfully&quot;);
          
          InputStream input = clientSocket.getInputStream();
          InputStreamReader inputreader = new InputStreamReader(input);
          BufferedReader br = new BufferedReader(inputreader);
          String string = null;
          
          while( (string = br.readLine()) != null){
              System.out.println(string);
              System.out.flush();
          }
  
      }catch(Exception ex){
          ex.printStackTrace();
      }
      finally{
          try{
              clientSocket.close();
          }catch(IOException ex){
              ex.printStackTrace();
          }
      }
  }
  
  private static ServerSocketFactory getServerSocketFactory() throws NoSuchAlgorithmException{
      
      SSLServerSocketFactory ssf = null;
      try{
          KeyManagerFactory kmf;
          KeyStore ks;
      SSLContext ctx;
          
          char[] passphrase = &quot;123456&quot;.toCharArray();
          
          ctx = SSLContext.getInstance(&quot;TLS&quot;);
          kmf = KeyManagerFactory.getInstance(&quot;SunX509&quot;);
          ks = KeyStore.getInstance(&quot;JKS&quot;);
          
          ks.load(new FileInputStream(&quot;mySrvKeystore&quot;), passphrase);
          kmf.init(ks, passphrase);
          ctx.getServerSocketFactory();
          
          return ssf;
      }catch(Exception ex){
          ex.printStackTrace();
      }
      return null;
  }
  
  public static void main(String args[]){
      if(args.length != 0){
          port = Integer.parseInt(args[0]);
      }
      
      
      StabilireConexiune(port);
      while(true){
          ConectareClient();
      }
  }}
  </code></pre>
  <p>The client:</p>
  <pre><code>public class SSLClient {
  
  public static void main(String args[]) {
      conectare(&quot;127.0.0.1&quot;, 4000);
  }
  
  private static SSLSocket socket;
  
  public static void conectare(String host, int port) {
  
      try {
          SSLSocketFactory factory = (SSLSocketFactory) SSLClient.getSocketFactory();
          socket = (SSLSocket) factory.createSocket(host, port);
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
          String[] enable = {&quot;TLS_DH_anon_WITH_AES_128_CBC_SHA&quot;};
          socket.setEnabledCipherSuites(enable);
          String[] cipherSuites = socket.getEnabledCipherSuites();
          for (int i = 0; i &lt; cipherSuites.length; i++) {
              System.out.println(cipherSuites[i]);
          }
          socket.addHandshakeCompletedListener(new HandshakeCompletedListener() {
              public void handshakeCompleted(HandshakeCompletedEvent event) {
                  System.out.println(&quot;handshake done&quot;);
              }
          });
          socket.startHandshake();
          PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
          System.out.println(&quot;Give a message to the server...&quot;);
          String string = br.readLine();
          out.println(&quot;Message to the server...&quot; + string);
          out.println();
          out.flush();
      }catch(IOException ex){
          ex.printStackTrace();
      }
      finally{
          try{
              socket.close();
          }catch(IOException ex){
             ex.printStackTrace();
          }
      }
  }
  
  private static SocketFactory getSocketFactory(){
      SSLSocketFactory ssf = null; 
      try{
          SSLContext ctx;
          KeyManagerFactory kmf;
          KeyStore ks;
          
          char[] passphrase = &quot;123456&quot;.toCharArray();
          
          ctx = SSLContext.getInstance(&quot;TLS&quot;);
          kmf = KeyManagerFactory.getInstance(&quot;SunX509&quot;);
          ks = KeyStore.getInstance(&quot;JKS&quot;);
          
          ks.load(new FileInputStream(&quot;mySrvKeystore&quot;), passphrase);
          kmf.init(ks, passphrase);
          ctx.init(kmf.getKeyManagers(), null, null);
          ssf = ctx.getSocketFactory();
          
          return ssf;
      }catch(Exception e){
          e.printStackTrace();
      }
      return null;
  }}
  </code></pre>
  <p>Please help me, what is the problem?</p>