Search code examples
jakarta-eeejb

Getting NameNotFoundException while running client program of EJB


I am new to EJB.

I am trying to learn to develop a stateless bean. I created the jar file and deployed it on Weblogic server and then i executed the client code. I have already set the classpath for api.jar and weblogic.jar. But on running client code i am getting following error:

C:\Users\Asad\Desktop\EJB>java EjbClient1
Exception in thread "main" javax.naming.NameNotFoundException: Unable to resolve 'myAdder#Adder'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'myAdder#Adder'. Resolved '']; remaining name 'myAdder#Adder'
        at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1224)
        at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:273)
        at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:217)
        at weblogic.jndi.internal.BasicNamingNode.lookupIgnorePartition(BasicNamingNode.java:1503)
        at weblogic.jndi.internal.PartitionHandler.lookupSharable(PartitionHandler.java:88)
        at weblogic.jndi.internal.ServerNamingNode.lookup(ServerNamingNode.java:584)
        at weblogic.jndi.internal.RootNamingNode.lookup(RootNamingNode.java:81)
        at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)
        at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:645)
        at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:248)
        at weblogic.rmi.internal.BasicServerRef$2.run(BasicServerRef.java:534)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:368)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:163)
        at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:531)
        at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:137)
        at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
        at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
        at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
        at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
        at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:617)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:397)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)
Caused by: javax.naming.NameNotFoundException: Unable to resolve 'myAdder#Adder'. Resolved ''
        at weblogic.utils.StackTraceDisabled.unknownMethod()

Following is the code.

Client EjbClient1.java:

import javax.naming.*;
import java.util.*;
import javax.naming.Context;  
import javax.naming.InitialContext;  

public class EjbClient1 {
    public static void main(String s[])throws Exception {
        Properties parm=new Properties();
        parm.setProperty("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
        parm.setProperty("java.naming.provider.url","t3://localhost:7001");
        parm.setProperty("java.naming.security.principal","weblogic");
        parm.setProperty("java.naming.security.credentials","asad9711");
        // parm.setProperty("java.naming.security.credentials","weblogic12");

        InitialContext ctx=new InitialContext(parm);
        Adder stub=(Adder)ctx.lookup("myAdder#Adder");

        int c=stub.add(10,20);
        System.out.println(c);

    }
}

Bean class AdderBean.java:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;

@Stateless(mappedName="myAdder")
class AdderBean implements Adder {
    @PostConstruct  
    public void init()
    {
        System.out.println("post create");

    }
    @PreDestroy
    public void destroy()
    {
        System.out.println("destroy");
    }
    public int add(int x,int y)
    {
        return x+y;
    }
}

Remote Interface Adder.java:

import javax.ejb.Remote;

@Remote
public interface Adder
{
     int add(int x,int y);
}

Solution

  • Your code works for me after changing few things.

    I changed your session bean's access modifier to public.

    Check your jar file contains the class files. It must contain your Interface and SessionBean.

    Screenshot with output

    With Command line

    1)Compile the project

    Project_DIR>javac -d bin -sourcepath src -cp lib\.m2\repo
    sitory\javax\javaee-api\6.0\javaee-api-6.0.jar;C:\Oracle\Middleware\wlserver_10.
    3\server\lib\weblogic.jar src\main\java\Adder.java src\main\java\AdderBean.java
    src\main\java\EjbClient1.java
    

    2)Build jar

    Project_DIR\bin>jar cf myejbapp.jar Adder.class AdderBean
    .class EjbClient1.class
    

    3)Deploy the myejbapp.jar to weblogic and run the EJBClient1