ctx = new InitialContext();
MyBean hello = (MyBean) ctx.lookup("MyBean");
However, if you want to use this code in a POJO style, you won’t be successful, looking up that JNDI. In the first cases, you were running your code within a container, thus you don’t need any specific configuration and the container will take care of it. However, in a POJO style (i.e. using main() method), you need to have some specification detailed out. One need to set up server properties to get the InitialContext.
For JBoss, you need to have the following set and be passed to your InitialContext:
Hashtable env = new Hashtable();
Context ctx = new InitialContext(env);
You also can create a jndi.properties files with the following parameters, and place them in your class-path.