i'm using
ejb 2 with myeclipse 9 and weblogic 9 . i'm learning
java ee. was trying to use myeclipse.just
testing with ejb2 have not yet started ejb3. getting following error when i run my client app.
error:
Exception in
thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an
applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at WishClient.main(WishClient.java:13)
my files
Wish.java
import java.rmi.RemoteException;
public interface Wish
extends javax.ejb.EJBObject
{
public
String getMonthName() throws RemoteException;
public String getDayName() throws RemoteException;
public String generateWishMsg(String name) throws RemoteException;
}
WishHome.java
public interface WishHome
extends javax.ejb.EJBHome
{
// public static final String COMP_NAME="java:comp/env/ejb/Wish";
// public static final String JNDI_NAME="Wish";
public Wish create()
throws javax.ejb.CreateException,java.rmi.RemoteException;
}
WishClient.java
import java.util.*;
import javax.naming.*;
public class WishClient {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Hashtable ht= new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
InitialContext ic = new InitialContext();
WishHome hor=(WishHome)ic.lookup("WishJndi");
Wish eor=hor.create();
System.out.println("Week Day Name =" +eor.getDayName());
System.out.println("Month Name =" +eor.getMonthName());
System.out.println("Wish Message =" +eor.generateWishMsg("Raj"));
}
}
WishBean.java
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.util.*;
public class WishBean implements SessionBean {
/** The session context */
private SessionContext context;
public void ejbCreate(){
System.out.println("ejbCreate()");
}
public WishBean() {
// TODO Auto-generated constructor stub
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
}
public String getMonthName(){
System.out.println("getMonthName()");
String months[]=new String[]{"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
Calendar cl=Calendar.getInstance();
int m=cl.get(Calendar.MONTH);
return months[m];
}
public String getDayName(){
System.out.println("getDayName()");
String weeks[]=new String[]{"","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
Calendar cl=Calendar.getInstance();
int w=cl.get(Calendar.DAY_OF_WEEK);
return weeks[w];
}
public String generateWishMsg(String name) throws EJBException {
// rename and start putting your business logic here
System.out.println("generateWishMsg()");
Calendar cl=Calendar.getInstance();
int h=cl.get(Calendar.HOUR_OF_DAY);
if(h<=12)
return "Good Morning" + name;
else if(h<=16)
return "Good Afternoon" + name;
else
return "Good Evening" + name;
}
}
ejb-jar.xml
<ejb-jar >
<enterprise-beans>
<session >
<ejb-name>ABC</ejb-name>
<home>WishHome</home>
<remote>Wish</remote>
<ejb-class>WishBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
weblogic-ejb-jar.xml
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>ABC</ejb-name>
<jndi-name>WishJndi</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
1. can someone please tell me why i'm getting error. how should i resolve it.
2.when i'm using xdoclet to generate bean, home interface and xml files. it automatically generating jndi name. like this
public static final String COMP_NAME="java:comp/env/ejb/Wish";
public static final String JNDI_NAME="Wish";
so i commented them and used my own jndi name in weblogic -ejb-jar.xml. is it wrong to do that?. how come these variables have me declared in my home interface. why and how are they used. and i did not understand the structure --> java:comp/env/ejb/Wish .. what does it mean?. i'm a newbie . so please explain clearly.