wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Advice Client problem 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 "Advice Client problem" Watch "Advice Client problem" New topic
Author

Advice Client problem

Angus Rose
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Hi all,
I've just worked through the first chapter of HFEJB. I'm having trouble running the Advice Client. The stack trace is below. Any suggestions?

Thanks in advance

Angus

javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at AdviceClient.go(AdviceClient.java:21)
at AdviceClient.main(AdviceClient.java:12)
Z Zia
Greenhorn

Joined: Oct 03, 2004
Posts: 24
Hello I am facing same problem, and just posted this question on EJB forum of this site

So let me know, when you know the solution
vishwa venkat
Ranch Hand

Joined: Nov 22, 2003
Posts: 185
check the link -

http://www.developer.com/java/ejb/article.php/10931_2215571_4

u have to set the initial_context_factory and provider_url.
vishwa venkat
Ranch Hand

Joined: Nov 22, 2003
Posts: 185
As mentioned in the above article, try setting


set path=%J2EE_HOME%\bin;%PATH%
set classpath=%J2EE_HOME%\lib\j2ee.jar;%CLASSPATH%;.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Has the issue been resolved? I'd also worked through the 1st chapter's example & managed to get it working successfully.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Sumeet Gaikwad
Ranch Hand

Joined: Mar 07, 2002
Posts: 37
Hi Cheng Wei Lee,

Could you be so kind enough to provide the solution, what exactly you did to compile and run the AdviceClient.

Also Which server you are using.? is it RI ?

I compiled and deployed the bean successfully. But when am trying to compile the client as suggested in HF am getting the error as



can anybody suggest what to do?


Sumeet Gaikwad<br />SCJP, SCWCD<br />-----------------------------------------------------------------------------------<br />Nothing is impossible because it spells... I M Possible !!!
Sumeet Gaikwad
Ranch Hand

Joined: Mar 07, 2002
Posts: 37
Hi,

Atlast compiled !!!

I refered Kathy's mails in this forum to compile the application. Thanks Kathy !!!

But while running the application am getting the error..

I refered all the mails regarding the same...but nobody got such exception....

I am running the application as



Can anybody help me out to come out of this problem??
Thanks in advance !!
Angus Rose
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Hi All,
I finally got it to run by including these lines in the AdviceClient:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory");
env.put(Context.PROVIDER_URL,"localhost:1050");
Context ic = new InitialContext(env);

Thanks to Visu Ramana for that one

I then got this exception:

java -cp "%CLASSPATH%;AdviceClient.jar" AdviceClient
Exception in thread "main" java.lang.NoClassDefFoundError: AdviceClient

Which I solved by changing it to:

java -cp "%CLASSPATH%;AdviceClient.jar";.; AdviceClient

i.e. I included the current directory in my classpath. Hope this helps, Sumeet.

Angus
Angus Rose
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Sorry - forgot about this:

After sorting the other 2 probs out, I then got the "java.rmi.AccessException: CORBA NO_PERMISSION 0 No" error but found this link had the answer.

http://java.sun.com/j2ee/sdk_1.3/1.3_01/faq.html#access
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Sorry Sumeet, been busy lately. Glad that you've managed to get it working.

Cheers!
Z Zia
Greenhorn

Joined: Oct 03, 2004
Posts: 24
I am still facing following error, before looking at error plz confirm:

1. IN J2EE RI version 1.4, we set JNDI reference in Bean's 'EJB Refs' Tab ?


"E:\projects\advice>java -classpath e:/projects/advice;c:/sun/AppServer/lib/j2ee.
jar;AdviceAppClient.jar AdviceClient
javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterp
rise.naming.SerialInitContextFactory. Root exception is java.lang.ClassNotFound
Exception: com.sun.enterprise.naming.SerialInitContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:217)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.jav
a:42)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
49)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243
)
at javax.naming.InitialContext.init(InitialContext.java:219)
at javax.naming.InitialContext.<init>(InitialContext.java:195)
at AdviceClient.go(AdviceClient.java:27)
at AdviceClient.main(AdviceClient.java:18)"
Z Zia
Greenhorn

Joined: Oct 03, 2004
Posts: 24
So finally my code is working, I am writing my fix below for reference to anyone searching for same, specifically for followers of HFEJB.

Server: J2EE RI version 1.4
1. Check IIOP Port(s): Click Start > Programs > Sun Microsystems > J2EE 1.4 SDK > Admin Console (Under General Tab, see IIOP Ports)

2. In Deploytool, under Applications, Click your app name and on JNDI Names tab check JNDI Name of your application.

3. This is client code that works in my case:

"
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL,"iiop://localhost:3700"); //get port no from step 1
Context ic = new InitialContext(env);
Object o = ic.lookup("AdviceBean"); //get from step 2
AdviceHome home = (AdviceHome) PortableRemoteObject.narrow(o, AdviceHome.class);
Advice advisor = home.create();
System.out.println( advisor.getAdvice() );
}
catch(Exception e) {
e.printStackTrace();
}"

4. Compile your Client by adding 'path\j2ee.jar' and jar returned from deploytool in your classpath
5. Execute by including both of above in your classpath

Happy coding,
Sumeet Gaikwad
Ranch Hand

Joined: Mar 07, 2002
Posts: 37
Hi,

Thanks Angus Rose !!!

Finally its running successfully...



- Sumeet
Estelle Edwards
Greenhorn

Joined: Oct 25, 2004
Posts: 1
Hi all

I am also having problems trying to run this. I've successfully compiled all the classes, but get a "CORBA.BAD_OPERATION 0 No;" exception when running AdviceClient. Everything seems to execute okay until "getAdvice()" is called.

Not sure where to go from here. If anyone can advise, this would be much appreciated!
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,

First of all, thanks so much for everyone posting their help here! Thanks Angus, Z Zia for your tips. As we always used to say at Sun... it takes 10 minutes to write your first EJB and about three days to see if it actually works (since getting everything working the first time has so many possible points of failure). So thanks all for being patient.

Estelle, your current problem is going to be a fairly easy one to solve, since it's a known bug in the RI, and there's a clear solution. Here are some notes I made on this:
=====================

If you receive the dreaded CORBA.BAD_OPERATION error when you run the client, you may be experiencing a bug that exists in some versions of the J2EE Reference Implementation.


The R.I. bug is:
* You can't have a method name that has the same characters as the interface name. Remember, this is a bug in the RI, and NOT a rule from the EJB spec.



* In the Advice application, the interface currently looks like this:



public interface Advice extends EJBObject {
���public String getAdvice() throws RemoteException;
}



The problem is that getAdvice() contains the characters "Advice"
that match the interface name, and this *can* in some systems, cause
the bug. We can't specify exactly which combination of versions and OS
produces the problem, except to say that the Linux distribution of J2EE
1.3.1 on Max OSX 10.2 and 10.3 does not have a problem.



The FIX is:



1) Change the business method name to something else in the
interface:



public interface Advice extends EJBObject {
��public String getTheMessage() throws
RemoteException;
}



2) Change the business method implementation in the bean class to match.



3) Change the client code that calls the method, to reflect the new method name.



4) Undeploy the current version and cleanup the server by running:


"cleanup" (without the quotes) at the command-line


(this will undeploy your application and take the server back to the
state it was in when installed.)


* Delete your existing .ear file (and any .temp files,
if you see them in your projects or Advice directory)


5) Restart the server and restart deploytool


6) Recreate the application and the bean (using New -->
Enterprise bean) and start over.



7) Be sure to wear your 'lucky t-shirt' while doing this. The R.I
has been known to respond to some forms of superstition. Or maybe it's
just an urban legend...



8) Run the client again and voila! Success! Finally!

==============

good luck,
Kathy
X. Li
Ranch Hand

Joined: Jan 31, 2004
Posts: 61
Hi Sumeet Gaikwad,

I got the exactly the same problem as yours when compiling Advice Client (Advice Class not found).

Could you post how you (Kathy) solved this problem?

Thanks.

Li
X. Li
Ranch Hand

Joined: Jan 31, 2004
Posts: 61
Hi Sumeet Gaikwad,

I got the exactly the same problem as yours when compiling Advice Client (Advice Class not found).

Could you post how you (Kathy) solved this problem?

Thanks.

Li
X. Li
Ranch Hand

Joined: Jan 31, 2004
Posts: 61
I found the solution <a href="http://www.coderanch.com/t/158518/java-EJB-SCBCD/certification/Error-compiling-AdviceClient-java">By David</a>
ra su
Greenhorn

Joined: Nov 12, 2004
Posts: 1
Hi,

I have used the following client to invoke the EJB Deployed application using Sun Server. But getting the error message. I have attached the client code.

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, "iiop://localhost:3700");

Context initial = new InitialContext(env);

//Context initial = new InitialContext();
//Context myEnv = (Context)initial.lookup("java:comp/env");

/*Retrieve the Object Found to the name - MyConverter */
Object objref = initial.lookup("MyConverter");
//Object objref = myEnv.lookup("ejb/SimpleConverter");

/*Narrow the Reference to a Home Object which we need. ie., ConverterHome */
ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objref, ConverterHome.class);

/*Invoke the create method in home interface, to get the Remote Interface which has all the
business methods. These business methods are implemented in Beans class */
Converter currencyConverter = home.create();


When I compile the above getting the message that
java.lang.ClassCastException
at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(Porta
bleRemoteObject.java:293)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
at bClient.ConClient.main(ConClient.java:31)

Please let me know if any ideas why this is comingup. I have specified "MyConverter" in JNDI Name field of J2EE Server.
Dilip Gamage
Ranch Hand

Joined: Sep 09, 2004
Posts: 148
At the first I have faced trouble what you have faced

There was bug in Windows version of J2EE 1.3.1.b17- You can refer bug code also there(see at web site where you download it)

Please correct (Your code: component interface(Advice) )change the method name without haveing name "Advice" and make nessary changes in Bean class according to that.....
p p p

Check this Link (this link has worked example BUG AVOIDED)
http://www.wickedlysmart.com/HeadFirst/HeadFirstEJB/HeadFirstEJBIndex.html


Cheers<br />Dilip <br />SCJP 1.4 ,SCBCD 1.3
Daniel Salomons
Greenhorn

Joined: Oct 07, 2004
Posts: 5
java -cp "%CLASSPATH%;AdviceClient.jar" AdviceClient
Exception in thread "main" java.lang.NoClassDefFoundError: AdviceClient

I had the same problem as Sumeet Gaikwad, and was able to solve it with answer by Angus Rose, though I did it slightly different: I added the current directory to the claspath in my environment variables. It solves this problem once and forever.
Gr. Daniel
Gautam Newalkar
Greenhorn

Joined: Mar 26, 2004
Posts: 4
Here's what I did, and I got it working. Note that I'm using Linux and the J2EE app server from Sun which I installed in /usr/apps/appserver.



that's it. the appserv-rt.jar contains all the JNDI naming classes. No need to add any -D parameters on the command line or any Hashtables in your code.
[ April 11, 2005: Message edited by: Gautam Newalkar ]
Eddie Yan
Greenhorn

Joined: Jul 08, 2005
Posts: 1
for those having problem with the InitialContext exception while running AdviceClient, use solution from Gautam Newalkar.
Ram Prakash Radhakrishnan
Greenhorn

Joined: Jul 31, 2005
Posts: 1
For those who still have problems with InitialContext in j2ee 1.4, this might help.

While running the classpath should contain appserv-rt.jar, j2ee.jar and
the AdviceAppClient.jar.

//AdviceClient.java
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import headfirst.*;

public class AdviceClient {

public static void main(String [] args) {
System.setProperty("javax.rmi.CORBA.UtilClass",
"com.sun.corba.ee.impl.javax.rmi.CORBA.Util");
System.setProperty("org.omg.CORBA.ORBClass",
"com.sun.corba.ee.impl.orb.ORBImpl");
System.setProperty("org.omg.CORBA.ORBSingletonClass",
"com.sun.corba.ee.impl.orb.ORBSingleton");
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.cosnaming.CNCtxFactory");
System.setProperty(Context.PROVIDER_URL,
"iiop://localhost:3700");
new AdviceClient().go();
}

public void go() {
try {
Context initialContext = new InitialContext();
Object o = initialContext.lookup("Advisor");
AdviceHome home = (AdviceHome)PortableRemoteObject.narrow(
o, AdviceHome.class);
Advice advisor = home.create();
System.out.println(advisor.getAdvice());
} catch (ClassCastException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
Darth Coder
Greenhorn

Joined: Jul 09, 2006
Posts: 2
This last bit was helpful to me. I noticed that since I had to use J2EE 1.4 since it was impossible to find 1.3 I failed to get the JNDI name to become Advisor and the beans JNDI name was AdviceBean. I finally had success.

Thanks All
Darth Coder
Greenhorn

Joined: Jul 09, 2006
Posts: 2
Here is a blog of some helpful advice. This is a great book. I had to use J2EE 1.4 with it.

CLASSPATH environment variable value

C:\Sun\AppServer\lib\j2ee.jar;C:\Sun\AppServer\lib\appserv-rt.jar;

C:\Sun\AppServer\lib\appserv-rt.jar has the jndi.properties file
in it. Without it you get the:

javax.naming.NoInitialContextException: Cannot instantiate class:
com.sun.enterprise.naming.SerialInitContextFactory [Root exception
is java.lang.ClassNotFoundException:
com.sun.enterprise.naming.SerialInitContextFactory]

problem.


JAVA_HOME
C:\Program Files\Java\jre1.5.0_06

PATH
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
c:\Python22;C:\Program Files\ATI Technologies\ATI Control Panel;
c:\Program Files\Microsoft SQL Server\90\Tools\binn\;...

Include both of these paths in your path...
for whatever reason the JDK1.5 puts some executables in 07 and some in 06 directories

C:\Program Files\Java\jre1.5.0_06\bin;
C:\Program Files\Java\jdk1.5.0_07\bin;

C:\www\Apache2\bin;
C:\www\php5;
C:\www\mysql\bin;
C:\www\perl\bin;
C:\www\Tomcat5\bin


Use this syntax for compiling and running: Syntax "%ENV_NAME%"
gives the environment variable value.

javac -cp "%CLASSPATH%" -d ../classes headfirst\*.java
To include other jars do this:
javac -cp "%CLASSPATH%";otherjar1.jar;otherjar2.jar -d ../classes headfirst\*.java
To include classes in the current directory do this:
(Solves the Can't find AdviceClient class problem)
javac -cp "%CLASSPATH%";otherjar.jar;"." -d ../classes headfirst\*.java


java -cp "%CLASSPATH%";AdviceAppClient.jar;"." AdviceClient

I had to get J2EE 1.4 Sun successfully hid the 1.3 released mentioned in the book from me so I settled for 1.4 which is slightly different in the deploy tool. The "Enterprise Bean Name" field becomes the JNDI name and I could not find a way to change that.

Derby is the database that comes with J2EE 1.4.

Start and stop the J2EE server:
asadmin stop-domain domain1
asadmin start-domain domain1

J2EE 1.4 does not come with a j2ee bat file.

You look at the server startup and shutdown status in
the log file. I could not get the output to go to the CMD
window using --terse=false or --verbose=true. the log file is here:

C:\Sun\AppServer\domains\domain1\logs\server.log


Hope this helps...
Joy Mookerji
Ranch Hand

Joined: Jul 26, 2006
Posts: 49


Getting this run time error


SCJP 5<br />Brainbench Certified in C++<br />PMP<br />Dallas,TX
Afeef Mirza
Greenhorn

Joined: Jul 26, 2008
Posts: 1
Daniel Salomons wrote:java -cp "%CLASSPATH%;AdviceClient.jar" AdviceClient
Exception in thread "main" java.lang.NoClassDefFoundError: AdviceClient

I had the same problem as Sumeet Gaikwad, and was able to solve it with answer by Angus Rose, though I did it slightly different: I added the current directory to the claspath in my environment variables. It solves this problem once and forever.
Gr. Daniel


Hi,

Even though I added the current directory in the CLASSPATH variable, it gives me the noclassdeffounderror. i added j2ee.jar in the classpath, it gave me compilation errors. i had to add j2ee.jar in the command line only then it would accept. I am using Windows XP and setting the env variables. .Can anyone explain why the problem is occuring?

Thank you!
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Afeef,

Please DontWakeTheZombies.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Advice Client problem
 
Similar Threads
Head First EJB: 1st tut error
Couldn't launch the Advice example in HF EJB book
Head First EJB - Problem Running Client of 1st example of EJB.
Starting EJB..JNDI exception
Error while running Advice EJB from HF