File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Initial Context Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Initial Context Exception" Watch "Initial Context Exception" New topic
Author

Initial Context Exception

vinoth ar
Greenhorn

Joined: Jan 07, 2009
Posts: 21
hi friends,
i did stateful session bean. no compilation error are coming. bu i get big fat

javax.naming.No.InitialContextException:Need to specify class name in environment or system property, or as an applet parameter or inan application resource file:java.naming.factory.initial

I am breaking head to find this

Thank you
vino.


vino...
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Vino,

Looks like you are missing JNDI properties. Please look into the JNDI documentation for your application server to see what you need to set these properties to.

Hope it helps,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
I think you are trying to access a Resource which is on a Remote Machine .For this you need to set the environmental properties of it using any Map implemented class and pass it to the InitialContext().
anoop estuate
Greenhorn

Joined: Mar 12, 2009
Posts: 4
Hi,
I have given all the details about the program. The jndi name in sun-ejb jar.xml file should match with the
//***********************************************************
sun-ejb jar.xml
//***********************************************************
<ejb>
<ejb-name>AdviceBeanBean</ejb-name>
<jndi-name>Advisor</jndi-name>
</ejb>

and
//**************************************************************
The client program
//**************************************************************

Object o = ic.lookup("Advisor");

//***************************************************************
Still why it is showing the error
//***************************************************************
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:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.client.AdviceClient.main(AdviceClient.java:29)


CAN ANY ONE HELP ME TO SOLVE THIS PROBLEM PLEASE!!!
Thanks in advance.....
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Can you post your client code ??


If you want something you never had do something which you had never done
anoop estuate
Greenhorn

Joined: Mar 12, 2009
Posts: 4
//***********************************************************************************
This is my client code
//************************************************************************************
package com.client;

import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.headfirst.*;
import javax.ejb.*;
import java.util.*;
import java.io.*;



/**
*
* @author ESTUATE
*/
public class AdviceClient
{
public static void main(String arg[])
{
try
{
Context ic = new InitialContext();
Object o = ic.lookup("Advisor");
AdviceBeanRemoteHome home = (AdviceBeanRemoteHome)PortableRemoteObject.narrow(o, AdviceBeanRemoteHome.class);
AdviceBeanRemote advisor = home.create();
System.out.println(advisor.getAdvice());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//***************************************************************************************
ejb-jar.xml
//***************************************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>EJBModule1</display-name>
<enterprise-beans>
<session>
<display-name>AdviceBeanSB</display-name>
<ejb-name>AdviceBeanBean</ejb-name>
<home>com.headfirst.AdviceBeanRemoteHome</home>
<remote>com.headfirst.AdviceBeanRemote</remote>
<ejb-class>com.headfirst.AdviceBeanBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>AdviceBeanBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
//***************************************************************************************
sun-ejb jar.xml
//***************************************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>AdviceBeanBean</ejb-name>
<jndi-name>Advisor</jndi-name>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
//**************************************************************************************
AdviceBeanBean( Bean logic)
//**************************************************************************************
package com.headfirst;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
*
* @author ESTUATE
*/
public class AdviceBeanBean implements SessionBean {

private SessionContext context;
private String[] adviceStrings = {"anoop","aneesh","srihari","babu"};
// <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;

// TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
// TODO Add business methods or web service operations

/**
* @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
*/
public void setSessionContext(SessionContext aContext) {
context = aContext;
System.out.println("setSessionContext");
}

/**
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() {
System.out.println("ejbActivate");
}

/**
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() {
System.out.println("ejbPassivate");
}

/**
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() {
System.out.println("ejbRemove");
}

// </editor-fold>;

/**
* See section 7.10.3 of the EJB 2.0 specification
* See section 7.11.3 of the EJB 2.1 specification
*/
public void ejbCreate() {
// TODO implement ejbCreate if necessary, acquire resources
// This method has access to the JNDI context so resource aquisition
// spanning all methods can be performed here such as home interfaces
// and data sources.
System.out.println("ejbCreate");
}

// Add business logic below. (Right-click in editor and choose
// "EJB Methods > Add Business Method" or "Web Service > Add Operation")
public String getAdvice()
{
System.out.println("getAdvice");
int random = (int)(Math.random() * adviceStrings.length);
return adviceStrings[random];
}
}
//*******************************************************************************
The problem i am facing is in netbeans and the problem is as follows
//*******************************************************************************

init:
deps-jar:
compile-single:
run-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:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.client.AdviceClient.main(AdviceClient.java:29)
BUILD SUCCESSFUL (total time: 1 second)
//**************************************************************************************************
Note:- I have created the EJBObject and EJBHome class also, I am not including the code in this mail, but if any one is interested I will send that also, "PLEASE HELP ME TO SOLVE THIS PROBLEM". Thanks in advance
//***************************************************************************************************
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Read the exception what it is saying .
"javax.naming.No.InitialContextException:Need to specify class name in environment or system property, or as an applet parameter or inan application resource file:java.naming.factory.initial"

Create a Properties class with the environmental properties and pass it to the InitialContext();

Something similar to this :



But not his code :
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initial Context Exception