title: How to do a remote JNDI lookup from Spring Boot Client to GlassFish Server?
body: <p>I am starting with a working Spring Boot application, using the latest version of Spring Boot and embedded Tomcat. It contains a HomeController with an index method mapped to a corresponding .jsp in the webapp directory. </p>
  
  <pre><code>@RequestMapping(value={"/", "/home", "/home/index"})
  public ModelAndView index() {
      ModelAndView mv = new ModelAndView("/home/index");
      return mv;
  }
  </code></pre>
  
  <p>My goal is to do a remote JNDIlookup to a glassfish server to access an EJB Singleton Service. I have previously verified using a simple desktop client that Remote JNDI lookups to my Glassfish server are working perfectly.</p>
  
  <p>I followed this tutorial for WildFly, repurposing it for Glassfish:
  <a href="https://www.baeldung.com/spring-ejb" rel="nofollow noreferrer">https://www.baeldung.com/spring-ejb</a></p>
  
  <p>Seemed simple enough. Instead of WildFly's client module, I would import Glassfish's, and instead of WildFly's InitialContext properties, I would use Glassfish's. </p>
  
  <p>So I updated my pom.xml with this (I also added a dependency containing remote interfaces):</p>
  
  <pre><code> &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.main.appclient&lt;/groupId&gt;
        &lt;artifactId&gt;gf-client&lt;/artifactId&gt;
        &lt;version&gt;5.1.0&lt;/version&gt;
   &lt;/dependency&gt;
  </code></pre>
  
  <p>Updated McvConfig.java like this (my base port for GlassFish is 8000, and it adds 37 for the JNDI port):</p>
  
  <pre><code> @Bean
   public Context ejbInitialContext() {
        InitialContext initialContext = null;
        try {
             Properties jndiProps = new Properties();
             jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
              "com.sun.enterprise.naming.impl.SerialInitContextFactory");
             jndiProps.put(Context.STATE_FACTORIES,
              "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
             jndiProps.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
             jndiProps.put("org.omg.CORBA.ORBInitialHost", "localhost");
             // optional.  Defaults to 3700.  Only needed if target orb port is not 3700.
             jndiProps.put("org.omg.CORBA.ORBInitialPort", "8037");
             initialContext = new InitialContext(jndiProps);
        } catch (NamingException e) {
        } 
        return initialContext;
   }
  </code></pre>
  
  <p>And Updated HomeController like this:</p>
  
  <pre><code>@Autowired
  Context ejbInitialContext;
  
  @RequestMapping(value={"/", "/home", "/home/index"})
  public ModelAndView index() {
      ModelAndView mv = new ModelAndView("/home/index");
      ServiceRemote service = (ServiceRemote) ejbInitialContext.lookup("java:global/core/Service");
      mv.addObject("Hello", service.hello());
      return mv;
  }
  </code></pre>
  
  <p>I ran the GlassFish server, and then tried to run my Spring Boot app. The Sprint Boot logs did not show up, and after a while, I got this instead (Full Log Here: <a href="https://pastebin.com/E11xLmFN" rel="nofollow noreferrer">https://pastebin.com/E11xLmFN</a>):</p>
  
  <pre><code> org.omg.CORBA.COMM_FAILURE: FINE: 00410001: Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700  vmcid: OMG  minor code: 1  completed: No
   ...
   Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused: connect
   ...
   Caused by: java.net.ConnectException: Connection refused: connect
  </code></pre>
  
  <p>I tried accessing localhost and it just said:</p>
  
  <pre><code>This site can’t be reached
  </code></pre>
  
  <p>I messed around with it for a while, and concluded a few things:</p>
  
  <ol>
  <li>Adding the new dependency was all it took to break Spring MVC and take down my web service.</li>
  <li><p>Spring is trying to integrate with GlashFish, because the stack trace includes this:</p>
  
  <p>at org.springframework.jndi.JndiTemplate.lambda$lookup$0(JndiTemplate.java:157)
  at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:92)
  at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:157)
  at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)</p></li>
  <li><p>Spring is completely ignoring my Bean and using the default GlassFish InitialContext, because the port number in the logs is still 3700, the default for GlassFish, but I specified 8048.</p></li>
  </ol>
  
  <p>Spring is somehow detecting the client module and periodically attempting to connect to Glassfish. The problem is that I have absolutely no idea how to tell Spring to use my custom InitialContext properties.</p>
  
  <p>I looked all over the web for a solution. Most of the similar questions were not asked very well, and received no answers, and others were specifically about the DataSource, which is too specific for me, as I am trying to put the entire Service layer on a Jakarta EE server.</p>