This discussion is archived
1 Reply Latest reply: Apr 7, 2012 1:18 PM by dvohra21 RSS

EJB Client giving error

922204 Newbie
Currently Being Moderated
Hi All,
I am new to EJB programming. I am trying to use EJB3 and Weblogic12c.

and would like to know why I am getting this error:
run:
[echo] Executing client class
[java] java.lang.NoClassDefFoundError: com/saurabh/demo/HelloWorldClient
[java] Caused by: java.lang.ClassNotFoundException: com.saurabh.demo.HelloW
orldClient
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[java] Could not find the main class: com.saurabh.demo.HelloWorldClient. P
rogram will exit.
[java] Exception in thread "main"
[java] Java Result: 1


I am posting my code so that its more easy to debug...

1. HelloWorld.java -

package com.saurabh.demo;
import javax.ejb.Remote;
@Remote
public interface HelloWorld
     {
     public void sayHello(String name);
     }

2. HelloWorldBean.java

package com.saurabh.demo;
import javax.ejb.Stateless;
@Stateless(mappedName="HelloWorld")
public class HelloWorldBean implements HelloWorld
     {
     public void sayHello(String name)
          {
          System.out.println("Hello "+name+ " Its Working");
          }

3.
package com.saurabh.demo;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
public class HelloWorldClient{

     private static HelloWorld helloworld;
     public static void main(String[] args)
     {
     try{
          Hashtable<String, String> env = new Hashtable<String,String>();
          env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
          env.put(Context.SECURITY_PRINCIPAL,"username of my weblogic");
          env.put(Context.SECURITY_CREDENTIALS,"password of my weblogic");
          env.put(Context.PROVIDER_URL,"t3://localhost:7001");
          Context ctx=new InitialContext(env);
          System.out.println("Initial Context created");
          helloworld =(HelloWorld)ctx.lookup("HelloWorld#com.saurabh.demo.HelloWorld");
          System.out.println("lookup Successful");
          System.out.println("Calling EJB method...");
          helloworld.sayHello("Saurabh");
          System.out.println("Output will be in Managed Server Console");
     }
     catch(Exception e)
     {
          e.printStackTrace();
     }
}
}
}

4. build.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<project name="Ejb_HelloWorld" basedir="." default="buildEar">

<property file="${basedir}/developer.properties"/>
<property name="jarname" value="HelloWorld-ejb"/>

<property name="appname" value="HelloWorld"/>

<property name="src" value="${basedir}/src"/>
<property name="build" value="${basedir}/build"/>
<property name="dist" value="${basedir}/dist"/>
<property name="lib" value="${basedir}/lib"/>

<property name="src.server" value="${src}/server"/>
<property name="src.client" value="${src}/client"/>
<property name="build.server" value="${build}/server"/>
<property name="build.client" value="${build}/client"/>
<property name="dist.server" value="${dist}/server"/>
<property name="dist.client" value="${dist}/client"/>

<path id="dependencies">
     <pathelement location="${lib}/javax.ejb_3.1.0.jar"/>
     <pathelement location="${lib}/weblogic.jar"/>
</path>

<target name="clean">
     <echo>"Cleaning the directories"</echo>
     <delete dir="${build}"/>
     <delete dir="${dist}"/>
</target>

<target name="compile" depends="clean">
     <echo>"Compiling EJB"</echo>
     <echo message="BEA_HOME:${BEA_HOME}"/>
     <echo message="WLS_HOME:${WLS_HOME}"/>
     <mkdir dir="${build}/server/classes"/>
     <echo message="Source directory : ${src.server}"/>
     <javac includeantruntime="false" srcdir="${src.server}" destdir="${build.server}/classes" debug="on">
     <classpath refid="dependencies"/>
     </javac>
</target>

<target name="compileClient">
     <echo>"Compiling Client class"</echo>
     <mkdir dir="${build.client}/classes"/>
     <javac includeantruntime="false" srcdir="${src.client}" destdir="${build.client}/classes" debug="on">
     <classpath refid="dependencies"/>
     <classpath location="${build.server}/classes"/>
     </javac>
</target>

<target name="buildEar" depends="compile">
     <echo>"Building EJB EAR"</echo>
     <mkdir dir="${dist.server}"/>
     <jar jarfile="${dist.server}/${jarname}.jar" basedir="${build}/server/classes"/>
     <jar jarfile="${dist.server}/${appname}.ear" basedir="${dist.server}"/>
</target>

<target name="run" depends="compileClient">
     <echo message="Executing client class"></echo>
     <java classname="com.saurabh.demo.HelloWorldClient" fork="yes">
     <classpath>
     <pathelement location="{build.client}/classes"/>
     <pathelement location="{dist.server}/HelloWorld-ejb.jar"/>
     <pathelement location="{lib}/wlfullclient.jar"/>
     </classpath>
     </java>
</target>
</project>

5. developer.properties- This property file is being used to set the needed paths.
BEA_HOME=C:\SG_Installs\wls1211_dev
JAVA_HOME=C:\SG_Installs\Java
WLS_HOME=C:\SG_Installs\wls1211_dev\wlserver
project.home=C:\Saurabh Data\WebDev\EJB-HelloWorld
wls.domain.path=C:\SG_Installs\BEA_Domain\BEA_Domain
wls.application.path=C:\SG_Installs\BEA_Domain\BEA_Domain\applications

There is no deployment descriptors since there is no need for that in EJB3 as I know.
I have been trying to resolve this issue but with no luck... Please help....

Edited by: 919201 on Mar 6, 2012 4:31 PM

Edited by: 919201 on Mar 6, 2012 5:54 PM

Edited by: Passionate on Mar 8, 2012 9:35 PM
  • 1. Re: EJB Client giving error
    dvohra21 Oracle ACE
    Currently Being Moderated
    java.lang.NoClassDefFoundError: com/saurabh/demo/HelloWorldClient
    +[java] Caused by: java.lang.ClassNotFoundException: com.saurabh.demo.HelloW+
    orldClient

    Does {build.client}/classes include the com/saurabh/demo/HelloWorldClient class?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points