Ask the Expert

Why is this code failing when used in a Java application on another box?

The following code returns the InitialContext just fine with a Java servlet, but fails when used in a Java application on another box. Can you tell me why?
InitialContext ctx = new InitialContext();
Object obj = ctx.lookup("com.borg.MyHome");
MyHome  home = (MyHome) PortableRemoteObject.narrow(obj, MyHome.class);

    Requires Free Membership to View

The reason that the snippet of code you have provided works in a servlet environment, is because the context is local and "known" without any additional information and thus can be retrieved by simply calling the no-arg constructor of the InitialContext class. However, when retrieving the context from another box, you must supply additional information in the form of properties to the InitialContext class. This additional information is configured for a WebLogic environment, as follows:
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.T3InitialContextFactory");
props.put(Context.PROVIDER_URL, "t3://somehostname:someportnumber");
InitialContext ctx = new InitialContext(props);
Object obj = ctx.lookup("com.borg.MyHome");
MyHome  home = (MyHome) PortableRemoteObject.narrow(obj, MyHome.class);

This was first published in April 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: