It's not a secret anymore!*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Problem with code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Problem with code" Watch "Problem with code" New topic
Author

Problem with code

Higor Zardo
Ranch Hand

Joined: Feb 16, 2008
Posts: 59
Hello ranchers,
I have had problems with my code in Netbeans 6.5 with Glassfish.

I have the bean

package exemplo.Stateless;

import javax.ejb.Stateless;

/**
*
* @author Higor
*/
@Stateless
public class HelloUserBeanBean implements HelloUserBeanRemote {

public void sayHello(String name) {
System.out.println("Hello " + name + " welcome to EJB 3 In Action!");
}

}


I have the Remote....


package exemplo.Stateless;

import javax.ejb.Remote;

/**
*
* @author Higor
*/
@Remote
public interface HelloUserBeanRemote {
public void sayHello(String name);
}



And I have a test client that use JNDI...


public class HelloUserClient {
//@EJB
public static HelloUserBeanBean helloUser;

public static void main(String[] args) {

//helloUser.sayHello("Curious George");
System.out.println("Invoked EJB successfully .. see server console for output");

try {
InitialContext ctx = new InitialContext();
HelloUserBeanBean bean = (HelloUserBeanBean) ctx.lookup("ejb/HelloUserBeanBeanJNDI");
helloUser.sayHello("Billy Bob");
//System.out.println(result);
System.out.println("testeeeeeeeeeee");
}
catch(Exception ex) {
ex.printStackTrace();
}

}
}




Ok, but when I execute the client I have the follow errors.

Invoked EJB successfully .. see server console for output
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 exemplo.Stateless.HelloUserClient.main(HelloUserClient.java:25)
BUILD SUCCESSFULLY (tempo total: 0 segundos)


Seem JNDI problem.
Anyone can Help me please?

Thank youuu,
Higor
rudresh kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 83
Hi,

you need to provide the jndi details, i am not sure for GlassFish, i use jboss, following are the details for the same

Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.clientrg.jnp.interfaces");
properties.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
InitialContext ctx = new InitialContext(properties);
Interface in = (Interface ) ctx.lookup("InterfaceBean/remote");

you need to add the above code in your client (modify for Glassfish) and add required jars in the classpath for the client

Thanks
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Rudresh,

I was facing the same problem! Thanks a lot for pointing it out! My client app is working now, but I get this in the console-


log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
log4j:WARN Please initialize the log4j system properly.


Also, how do we knoe that we need to use the code that you showed, if it is Jboss? Is it mentioned in any of it's docs? Please let me know the name of that doc.


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
rudresh kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 83
Hi,

For Glass fish try the below

Properties props=new Properties();
props.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");//ur server ip
props.setProperty("org.omg.CORBA.ORBInitialPort","3700"); //default is 3700

and for the errors, it is because of log4j configuration, you can configure the log4j. regarding the above entires, it is not required if you add jndi.properties in your classpath and make it part of the ear, even i was getting these errors intially, goggled it to resolve the same

Thanks
Higor Zardo
Ranch Hand

Joined: Feb 16, 2008
Posts: 59
Hello friends,
where do you put the code?
I put inside client. It's like:

package exemplo.Stateless;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
*
* @author Higor
*/
public class HelloUserClient {
//@EJB
public static HelloUserBeanBean helloUser;

public static void main(String[] args) {

//helloUser.sayHello("Curious George");
System.out.println("Invoked EJB successfully .. see server console for output");
Properties props=new Properties();
props.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");//ur server ip
props.setProperty("org.omg.CORBA.ORBInitialPort","3700"); //default is 3700

try {
InitialContext ctx = new InitialContext();
HelloUserBeanBean bean = (HelloUserBeanBean) ctx.lookup("ejb/HelloUserBeanBeanJNDI");
helloUser.sayHello("Billy Bob");
//System.out.println(result);
System.out.println("testeeeeeeeeeee");
}
catch(Exception ex) {
ex.printStackTrace();
}

}
}

The problem is that Properties dont exist and inside API Specification JEE it dont exist too.

Thank youuu,
Higor
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
change your code to like this.......

Hashtable ht=new Hashtable();
ht.put("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
ht.put("org.omg.CORBA.ORBInitialHost", "localhost");
ht.put("org.omg.CORBA.ORBInitialPort","3700");


Context ctx=new InitialContext(ht);

Higor Zardo
Ranch Hand

Joined: Feb 16, 2008
Posts: 59
Hello Nitin,
how are you?
Well, I tested it but a have had more problems.

I did it:

package exemplo.Stateless;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.*;
/**
*
* @author Higor
*/
public class HelloUserClient {
//@EJB
public static HelloUserBeanBean helloUser;

public static void main(String[] args) {

//helloUser.sayHello("Curious George");
System.out.println("Invoked EJB successfully .. see server console for output");

Hashtable ht=new Hashtable();
ht.put("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
ht.put("org.omg.CORBA.ORBInitialHost", "localhost");
ht.put("org.omg.CORBA.ORBInitialPort","3700");
//Context ctx=new InitialContext(ht);

try {
//InitialContext ctx = new InitialContext();
Context ctx=new InitialContext(ht);
//HelloUserBeanBean bean = (HelloUserBeanBean) ctx.lookup("ejb/HelloUserBeanBeanJNDI");
helloUser = (HelloUserBeanBean) ctx.lookup("ejb/HelloUserBeanBeanJNDI");
helloUser.sayHello("Billy Bob");
//System.out.println(result);
System.out.println("testeeeeeeeeeee");
}
catch(Exception ex) {
ex.printStackTrace();
}

}
}


And I received it...


init:
deps-jar:
compile-single:
run-main:
Invoked EJB successfully .. see server console for output
javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at exemplo.Stateless.HelloUserClient.main(HelloUserClient.java:32)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
... 4 more
BUILD SUCCESSFULLY (tempo total: 0 segundos)




It is very dificult to execute yet. If anyone can help me thank you.

Thank youuu,
Higor
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You should not need to pass anything to the InitialContext. Check this FAQ.


[My Blog]
All roads lead to JavaRanch
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
make sure you have these two jar in your classpath. appserv-rt.jar,javaee.jar

As christopher is saying try without passing parameter to initialcontext.

Best of luck
Higor Zardo
Ranch Hand

Joined: Feb 16, 2008
Posts: 59
Hello friends Christophe Verré and Nitin!!!
Thank you for help me.

Well, I tried without parameter:

package exemplo.Stateless;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.*;
/**
*
* @author Higor
*/
public class HelloUserClient {
//@EJB
public static HelloUserBeanBean helloUser;

public static void main(String[] args) {

//helloUser.sayHello("Curious George");
System.out.println("Invoked EJB successfully .. see server console for output");

Hashtable ht=new Hashtable();
ht.put("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
ht.put("org.omg.CORBA.ORBInitialHost", "localhost");
ht.put("org.omg.CORBA.ORBInitialPort","3700");
//Context ctx=new InitialContext(ht);

try {
Context ctx=new InitialContext();
helloUser = (HelloUserBeanBean) ctx.lookup("HelloUserBeanBean");
helloUser.sayHello("Billy Bob");
System.out.println("testeeeeeeeeeee");
}
catch(Exception ex) {
ex.printStackTrace();
}

}
}



And I received some errors yet:

init:
deps-jar:
Compiling 1 source file to C:\Users\Higor\Documents\NetBeansProjects\EJBModule2\build\jar
Note: C:\Users\Higor\Documents\NetBeansProjects\EJBModule2\src\java\exemplo\Stateless\HelloUserClient.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
compile-single:
run-main:
Invoked EJB successfully .. see server console for output
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 exemplo.Stateless.HelloUserClient.main(HelloUserClient.java:32)
BUILD SUCCESSFULLY (tempo total: 0 segundos)



Thank youuu,
Higor
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you follow the exact procedure explained in the FAQ ? Try to do it again from scratch.
 
Don't get me started about those stupid light bulbs.
 
subject: Problem with code
 
Similar Threads
EJB3 @EJB annotation not loading Session bean
JNDI naming and DI in EJB 3
How to run Hello World example in Eclipse with Glassfish v3 as appserver
exception in first ejb 3.1 program on NetBean 6.1
EJB simple client is not running