File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB# lookup() basic

 
Juan Handal
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which application server are you using? You are missing the jndi.properties from the client classpath. This file contains server specific jndi related information.
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.naming.NoInitialException


I am guessing its, javax.naming.NoInitialContextException
 
Venkatesh Rangamani
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Jaikiran is right... you need appserv-rt.jar file in your client's classpath....
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic