While you can use JPA (which is part of EJB 3) "Entity Beans" (actually, POJOs) in a J2SE environment, you can't use Session Beans without a container and you can't benefit from injection of resources using the @Resource
or the more specialized @EJB
and @WebServiceRef
annotations in a non-managed environment, i.e. a container. In other words, only managed components support injection (Servlets, JSF Managed beans, EJB components, etc).
So, in your case, you'll need to:
Deploy your Session Bean in a Java EE container (like JBoss, GlassFish, WebLogic, etc)
Lookup the remote EJB using explicitly its global JNDI name. The code will look like that:
Foo foo = (Foo) new InitialContext().lookup("FooEJB");
A few additional remarks:
- With EJB 3.0, the global
JNDI
name is container dependent so I can't tell you what it will be exactly (EJB 3.1 finally introduced "portable global JNDI
names").
- You'll need to set up the JNDI environment (which is container dependent) for the lookup either by providing a
jndi.properties
on the class path or by using the InitialContext(Hashtable)
constructor.
- You'll need to provide the application server "client library" on the class path of the client (which is obviously specific to each product).
Search for previous questions or open a new one if you need more specific guidance.