Exception running ejb client locally
c.kulkarni Jul 12, 2012 11:01 AMI am getting following:
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.mypackage.client.EJBApplicationClient.doLookup(EJBApplicationClient.java:28)
at com.mypackage.client.EJBApplicationClient.main(EJBApplicationClient.java:14)
Exception in thread "main" java.lang.NullPointerException
at com.mypackage.client.EJBApplicationClient.main(EJBApplicationClient.java:15)
=======================================================
jboss-client.properties is as below:
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
=======================================================
Here is EJBApplicationClient:
package com.mypackage.client;
import javax.naming.Context;
import javax.naming.NamingException;
import com.mypackage.Calculator;
import com.mypackage.CalculatorBean;
import com.mypackage.clientutility.ClientUtility;
public class EJBApplicationClient {
public static void main(String[] args) {
Calculator bean = doLookup();
System.out.println(bean.add(2, 3)); // 4. Call business logic
}
private static Calculator doLookup() {
Context context = null;
Calculator bean = null;
try {
// 1. Obtaining Context
context = ClientUtility.getInitialContext();
bean = (Calculator) context.lookup("java:module/CalculatorBean!com.mypackage.CalculatorLocal");
} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
}
=====================================
Client Utility is as below:
package com.mypackage.clientutility;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ClientUtility {
private static Context initialContext;
private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";
public static Context getInitialContext() throws NamingException {
if (initialContext == null) {
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
initialContext = new InitialContext(properties);
}
return initialContext;
}
}
====================================================
Calculator Bean is as below:
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.mypackage;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote(CalculatorRemote.class)
@Local(CalculatorLocal.class)
public class CalculatorBean implements CalculatorRemote, CalculatorLocal
{
public int add(int x, int y)
{
return x + y;
}
public int subtract(int x, int y)
{
return x - y;
}
}
==============================
Thanks in advance.