Skip to Content
author's profile photo Former Member
Former Member

A Dumb Question About the Calculator Tutorial

Calculator deployed and worked as it was supposed to, but what if I wanted to test without messing with JSP?

I put the following into


public static void main(String[] args) throws Exception {

CalcProxy proxy = new CalcProxy();

String ADD = "3";




But when I try to run the application, I get:

CalcProxy.init error in context lookup: 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

So what do I need to do to make this work?


Message was edited by: Dale Seng

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Posted on Jun 14, 2004 at 01:09 PM

    I thinik you shopuld have a deeper look into what you're doing here. You cannot change a server application into a standalone one by just adding a main method.

    I don't know what it is specially, but you surely are unaware about some J2EE facts...



    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Benny Schaich-Lebek

      >> Benny, I understood that clients of EJB's can be any

      >> number of things, including a Java application. That

      >> would lead me to believe that indeed adding a main

      >> method is a valid way to interact with an EJB. If

      >> those are invalid statements, I'd like some

      >> enlightenment.>

      >The idea of the CalcProxy is to work as controller of an

      >JSP. This means it get's some parameters from there,

      >which are mandatory. Without them it will not do


      I just checked the file (this is built by the platform and it's the code that really runs when the page runs, right?). All it does is get a reference to the CalProxy and then it calls getResult on it, with parms from the page. I suspect that the getAttribute call will instantiate the CalProxy object if needed, since the constructor is NOT called in the JSP code. Here's the interesting stuff from the code:

      CalProxy proxy = (CalProxy)pageContext.getAttribute("proxy", PageContext.SESSION_SCOPE);


      So, as a client, the JSP did these two things. I suggest that any other client, should also be able to do these two things as well.

      In the original entry in this thread, I posted code that did these two things:

      CalcProxy proxy = new CalcProxy();

      String ADD = "3";


      The problem is that the environment in which the CalcProxy class runs is different when the container starts running as opposed to when I start CalcProxy from the Eclipse IDE. I think it's a matter of things missing from the classpath.

      The result is that during CalcProxy.init, things having to do with initial context and lookup do not work. Even when it is able to find, something is still not right with the initial context. But any client, anywhere on the network should be able to lookup the home object. That's what EJB is all about, yes? If it bothers you that the code is in the main of the controller, imagine it out in it's own class, on another machine... it would make no difference to the discussion.


      (this has got to be the longest anyone has ever spent trying to add 1 + 1) 😉

      Message was edited by: Dale Seng

  • author's profile photo Former Member
    Former Member
    Posted on Jun 17, 2004 at 05:21 PM

    This was easier than I thought!

    Early-on, I had figured I would need to alter the initial context so it could find the server:

    //InitialContext ctx = new InitialContext();

    Properties prop = new Properties();


    InitialContext ctx = new InitialContext(prop);

    And I knew I wouldn't be able to use the special EJB naming from an 'outside' client:

    //Object ob = ctx.lookup("java:comp/env/ejb/CalBean");

    Object ob = ctx.lookup("CalBean");

    But somehow the JNDI name that I put in (CalBean) got 'lost'. Once I put it back, it worked fine.

    Thanks to Benny for posting today and getting me to look at this again! And thanks to Astrid for putting up with my exploration of the classpath angle.


    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Stefan, Agreed. But you will need to define a name in JNDI. The tutorial doesn't show you exactly how to do it. I says only this:

      <i>Adding Descriptions to ejb-j2ee-engine.xml

      The deployment descriptor ejb-j2ee-engine.xml generally defines other properties specific to

      the SAP J2EE Engine. For example, you can specify the JNDI name of the beans in this file.

      However, these special entries are not required for this example application.</i>

      stefan.send(new Thanks());

      I like how you put your code in "code" tags!


Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.