5

Good day! I'm newcomer in the JavaEE 6. I tried to create a started project with use of the following tutorial: Creating and Running an Application Client on the GlassFish Server. I created four projects step by step: JavaSE lib to store an EJB Remote Interface, an EJB Module and otherwise as ther're described in the tutorial.

My platform:

  • Distributor ID: Ubuntu Description: Ubuntu 10.10 Release: 10.10 Codename: maverick
  • jdk1.6.0_26
  • Netbean7.2(Build 201207171143)
  • GlassFish Server 3+

My remote interface

    package ejb.started;
    //    
    import javax.ejb.Remote;
    //
    @Remote
    public interface StartedSessionBeanRemote {

        String getHelloWorld();

    }

Its implementation:

package ejb.started;
//
import javax.ejb.Stateless;
//
@Stateless(name="StartedSessionBeanRemote")
public class StartedSessionBean implements StartedSessionBeanRemote {

    @Override
    public String getHelloWorld() {
       return ("Hello world, EJB!");
    }

}

And the client code:

package startedentappclient;
//
import ejb.started.StartedSessionBeanRemote;
import javax.ejb.EJB;
//
public class AppClientMain {
    @EJB
    private static StartedSessionBeanRemote startedSessionBean;
    public static void main(String[] args) {
        System.err.println("EJB: "+startedSessionBean.getHelloWorld());
    }
}

When I try to run the Remote Client proj, I receive the following output:

ant -f /home/ilya/Workspace/git/workspace/StartedEntAppClient -DforceRedeploy=false run
init:
EJBRemoteInterface.init:
Deleting: /home/ilya/Workspace/git/workspace/EJBRemoteInterface/build/built-jar.properties
EJBRemoteInterface.deps-jar:
Updating property file: /home/ilya/Workspace/git/workspace/EJBRemoteInterface/build/built-jar.properties
EJBRemoteInterface.compile:
EJBRemoteInterface.jar:
deps-jar:
compile:
library-inclusion-in-archive:
dist:
pre-run-deploy:
Redeploying /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist/StartedEntAppClient.jar
Initializing...
post-run-deploy:
run-deploy:
Copying 1 file to /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist
Copying 2 files to /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist/StartedEntAppClientClient
Warning: /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist/gfdeploy/StartedEntAppClient does not exist.
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=startedentappclient.AppClientMain/startedSessionBean,Remote 3.x interface =ejb.started.StartedSessionBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=ejb.started.StartedSessionBeanRemote,refType=Session into class startedentappclient.AppClientMain: Lookup failed for 'java:comp/env/startedentappclient.AppClientMain/startedSessionBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl}
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectClass(InjectionManagerImpl.java:213)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectClass(InjectionManagerImpl.java:205)
    at org.glassfish.appclient.client.acc.AppClientContainer$ClientMainClassSetting.getClientMainClass(AppClientContainer.java:625)
    at org.glassfish.appclient.client.acc.AppClientContainer.getMainMethod(AppClientContainer.java:517)
    at org.glassfish.appclient.client.acc.AppClientContainer.completePreparation(AppClientContainer.java:411)
    at org.glassfish.appclient.client.acc.AppClientContainer.prepare(AppClientContainer.java:319)
    at org.glassfish.appclient.client.AppClientFacade.prepareACC(AppClientFacade.java:278)
    at org.glassfish.appclient.client.acc.agent.AppClientContainerAgent.premain(AppClientContainerAgent.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:323)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:338)
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/startedentappclient.AppClientMain/startedSessionBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=startedentappclient.AppClientMain/startedSessionBean,Remote 3.x interface =ejb.started.StartedSessionBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=ejb.started.StartedSessionBeanRemote,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' [Root exception is javax.naming.NamingException: Lookup failed for 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found]]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:599)
    ... 15 more
Caused by: javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=startedentappclient.AppClientMain/startedSessionBean,Remote 3.x interface =ejb.started.StartedSessionBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=ejb.started.StartedSessionBeanRemote,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' [Root exception is javax.naming.NamingException: Lookup failed for 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found]]
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:191)
    at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1109)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744)
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:169)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
    ... 18 more
Caused by: javax.naming.NamingException: Lookup failed for 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:186)
    ... 23 more
Caused by: javax.naming.NameNotFoundException: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
    at com.sun.enterprise.naming.impl.RemoteSerialContextProviderImpl.lookup(RemoteSerialContextProviderImpl.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:144)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:174)
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1624)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1486)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:990)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:214)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:742)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:539)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2324)
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
Java Result: 1
run:
BUILD SUCCESSFUL (total time: 11 seconds)

I can't assume what's that problem. I built same projects on my second PC and all work successfully. That one has the same software platform.

ilya8891
  • 127
  • 1
  • 1
  • 12
  • I found that tutorial flawed. They suggest to make an Enterprise Application. If, instead, you only make an EJB module it should work as advertised. See: http://stackoverflow.com/questions/25828542/how-to-create-a-remote-session-ejb-from-a-client/25829907#25829907 – Thufir Sep 20 '14 at 15:53

2 Answers2

4

When you write:

@EJB
private static StartedSessionBeanRemote startedSessionBean;

your client tries to perform an injection of that EJB. In your case that obviously wasn't successful:

com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject...

You can help yourself by using mappedName construct. Rewrite these lines of your code in the following way:

@Stateless(mappedName="ejb/StartedSessionBeanRemote")
public class StartedSessionBean implements StartedSessionBeanRemote {

and

@EJB(mappedName="ejb/StartedSessionBeanRemote")
private static StartedSessionBeanRemote startedSessionBean;

Finally, make sure that your EJB is deployed before the client, because without the EJB client cannot inject it :)

Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66
  • Yes, I forgot to deploy the app client and the enterprise app.) I corrected it and all began to work. Thanks! – ilya8891 Aug 31 '12 at 08:51
  • But it is now working for me. Yar..... I tried many examples but not injecting `Remote Ejb`. I went through https://netbeans.org/kb/docs/javaee/entappclient.html#Exercise_32 example – Yubaraj Jan 07 '14 at 05:50
4

I believe you cannot access an EJB using the @EJB annotation from a main method because that method is not managed by the server. Instead, you should use JNDI.

How do I access a Remote EJB component from a stand-alone java client?

carcaret
  • 3,238
  • 2
  • 19
  • 37
  • Thanks a lot. As I wrote, I'm incomer. So I will be thanksworthy for all the helpfull material relating to JavaEE and JavaBeans in the particular case. – ilya8891 Aug 31 '12 at 08:45
  • 1
    @carcaret: That is true in case of standalone Java application, but not in case of Enterprise Application Client, as mentioned in ilya8891's question. You can normally use injection here. – Miljen Mikic Aug 31 '12 at 13:49
  • 1
    @Thufir Check [this one](https://docs.oracle.com/cd/B14099_19/web.1012/b15505/access.htm). In the end you need to retrieve the EJB using JNDI. – carcaret Aug 19 '17 at 08:40
  • Interesting. Looking into `OC4J`, which seems to be a pared down app server (glassfish). How does this compare to `MDB` or even `CORBA`? Invoking EJB with some form of `RMI` looks thorny. There's no "easy" EJB to (remote) EJB technology? – Thufir Aug 19 '17 at 09:02
  • 1
    @Thufir As far as I know, if you're trying to invoke an EJB from a standalone application, i.e. a `main` method, since there's no EJB container managing the beans, the only way I know is to use JNDI. OC4J seems to be a container that can manage EJB beans, so I guess by using that you should be able to inject beans with the `@EJB` annotation, although I'm not familiar with it. In the end, to use annotations you need an EJB container (which all Java EE compliant servers have), otherwise you should retrieve them using other mechanisms, like JNDI. – carcaret Aug 19 '17 at 09:37