aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB# lookup() basic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB# lookup() basic" Watch "EJB# lookup() basic" New topic
Author

EJB# lookup() basic

Juan Handal
Greenhorn

Joined: Jan 25, 2005
Posts: 29
This a stand-alone client a POJO BTest.java trying a lookup() in
the Session Bean BBean.java the error is :
javax.naming.NoInitialContextException.
I need to fix the JNDI lookup()


A project
|
|
A-ejb
|
|
Source Packages
|
|
ejb
|
BBean.java
BRemote.java
BTest.java
Right click ejb New------>File/Folder----------->Java Classes-------->Java Main Class

Creates a new Java class with a main method permitting it to be run as a console
application.

The Remote interface(I tried also with Local interface ,same result) :

package ejb;

import javax.ejb.Remote;
public interface BRemote{ }






The Session Bean :

package ejb;
import javax.ejb.Stateless;
@Stateless(name="X")
public class BBean implements ejb.Remote{
public BBean(){ }
}



Is a useless Bean ,but my point is to try the Context with BTest.java







OK this Btest is a POJO so i can not use Direct Injection,I will try l to get the Context.

package ejb;
import java.util.loggin.Level;
import java.util.loggin.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class BTest{
public BTest(){ }
public static void main(String [] args){

BTest cc=new BTest();
BRemote dd=(BRemote)cc.lookupX();
}
private BRemote lookupX(){
try{
Context c=new InitialContext();
return (ejb.BRemote)c.lookup("java:comp/env/ejb/"X");
}catch(NamingException ne){

Logger.getLogger(getClass().getName().log(Level.SEVERE,"exception caught",ne);
throw new RuntimeException(ne);
}


}end of class





Then I deployed A project OK no problem
I go to Run File (Shift +F6) :Run BTest.java

I got:
ejb.cc lookupX

Severe: exception caught

javax.naming.NoInitialException


This lookupX() was generated by the IDE
right click in the BTest{} class-------Enterprise Resources--->Call Enterprise Bean

How can I get it right with Netbeans?

The tutorial is great and works,but in this simple code not.

Thanks.




Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Which application server are you using? You are missing the jndi.properties from the client classpath. This file contains server specific jndi related information.


[My Blog] [JavaRanch Journal]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

javax.naming.NoInitialException


I am guessing its, javax.naming.NoInitialContextException
Venkatesh Rangamani
Ranch Hand

Joined: Aug 24, 2005
Posts: 31
Juan,
What i think is java:comp/env is accessible only when we access the bean from the server side component say a JSP or another EJB. If you want to access the Bean from outside the Server like a main program then you need to have a specific name and cannot use java:comp/env. So change your JNDI name of the bean and do a plain lookup. Please let me know if this does/doesnt solve your issue.

Thanks
Venkat
Juan Handal
Greenhorn

Joined: Jan 25, 2005
Posts: 29
EVERE: exception caught
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 ejb.CC.lookupX(CC.java:34)
at ejb.CC.main(CC.java:27)
Exception in thread "main" java.lang.RuntimeException: 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 ejb.CC.lookupX(CC.java:38)
at ejb.CC.main(CC.java:27)
Caused by: 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 ejb.CC.lookupX(CC.java:34)
... 1 more
Java Result: 1
BUILD SUCCESSFUL (total time: 11 seconds)

Thanks Jaikiran and Venkatesh

I'm with GlassFish(APPServer 9)That comes bundled whit

Netbeans 5.5.1.

I tried @Stateless(name="XXX")
and the lookup("XXX") the same error.

As you say the jndi.properties have something to do whit the error.

I'm running Btest.java(Where lookup() is ) as a console application,

but inside the IDE and in the same ejb directory of the Stateless Bean.

Juan Handal
Greenhorn

Joined: Jan 25, 2005
Posts: 29
Hi

I read this article:


https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#AppClientDef


I think the answer is close but I don't hava a clear picture.


Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Quote from that link you posted:

The most common problem developers run into is passing specific JNDI bootstrapping properties to InitialContext(args). Stand-alone java clients that access Java EE services are by definition non-portable, so every Java EE implementation has different requirements for how to bootstrap the naming provider. (See here for more information on writing portable clients). Instead of requiring the developer to hard-code specific JNDI bootstrapping properties, we have put a jndi.properties file within appserv-rt.jar. The JNDI machinery in Java SE automatically detects this file when used in conjunction with no-arg InitialContext() and bootstraps the correct naming provider.


Do you have the appserv-rt.jar file in your client's classpath
Milan Jagatiya
Ranch Hand

Joined: Jan 01, 2007
Posts: 164
Yeah Jaikiran is right... you need appserv-rt.jar file in your client's classpath....


Milan.<br />I can because I think I can...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJB# lookup() basic