title: How to get initial context from GlassFish server in Java SE?
body: <p>I have a class like below:</p>
  
  <pre><code>public class Poligon {
  
      public static void main(String[] args) {
  
          try {
              Context ctx = new InitialContext();
              ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/javaee7/ConnectionFactory");
              Destination destination = (Destination) ctx.lookup("jms/javaee7/Topic");
              JMSContext context = connectionFactory.createContext();
              OrderDTO order = context.createConsumer(destination).receiveBody(OrderDTO.class);
              System.out.println("Order received: " + order);
          } catch (NamingException ex) {
              Logger.getLogger(Poligon.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p>I would like to get the InitialContext() form the server (glassfish) running on localhost, but I get the below error:</p>
  
  <pre><code>SEVERE: null
  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
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
      at javax.naming.InitialContext.lookup(InitialContext.java:411)
      at poligon.Poligon.main(Poligon.java:29)
  </code></pre>
  
  <p>I know I have to create ldap realm on glassfish and add the below code (? - dont know the exact values) to my class:</p>
  
  <pre><code>Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,
              "?");
      env.put(Context.PROVIDER_URL, "?");
      env.put(Context.SECURITY_PRINCIPAL, "?");
      env.put(Context.SECURITY_CREDENTIALS, "?");
  
  Context ctx = new InitialContext(env);
  </code></pre>
  
  <p>My problem is that I dont know what values should be at:</p>
  
  <pre><code>Context.INITIAL_CONTEXT_FACTORY
  Context.PROVIDER_URL (I want it on localhost)
  Context.SECURITY_PRINCIPAL
  Context.SECURITY_CREDENTIALS
  </code></pre>
  
  <p>And I dont know how I should configure glassfish server?</p>
  
  <p><img src="https://i.stack.imgur.com/yPY3H.png" alt="enter image description here"></p>
  
  <p>maven dependencies</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.main.extras&lt;/groupId&gt;
          &lt;artifactId&gt;glassfish-embedded-all&lt;/artifactId&gt;
          &lt;version&gt;4.0&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.main.appclient.client&lt;/groupId&gt;
          &lt;artifactId&gt;gf-client&lt;/artifactId&gt;
          &lt;version&gt;3.1.2.2&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>