Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNDI "Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory"

I'm using JBoss Server for EJB And I need JNDI in console app to get reference of session bean, console app code looks like this

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;


public class Program {

    public static void main(String[] args) throws NamingException {
        // TODO Auto-generated method stub
        Properties pr = new Properties();
        pr.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        pr.put(InitialContext.PROVIDER_URL,"remote://localhost:4447");
        InitialContext ic = new InitialContext(pr);

    }

}

when I run the application I get exception

Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.InitialContext.<init>(Unknown Source)
    at Program.main(Program.java:14)
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
    ... 5 more
like image 641
user3188370 Avatar asked Jan 19 '14 05:01

user3188370


3 Answers

To overcome above error you need to pay attention to two points.
First , You need to have jboss-client.jar in classpath.
Second, according to Jboss version you are using you need to change your provider url.

For JBossAS 5 you should set following properties in environment

env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");

For JBossAS 7 you should set following properties in environment

env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, "remote://localhost:4447"));
env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
like image 180
Jagdish Chandra Avatar answered Nov 16 '22 00:11

Jagdish Chandra


You can use following context to connect. I have tried and tested to set up this.

import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;


public class Program {

    public static void main(String[] args) throws NamingException {
      Properties jndiProps = new Properties();
    jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,         
    "org.jboss.naming.remote.client.InitialContextFactory");
    jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
    jndiProps.put(Context.SECURITY_PRINCIPAL, "testuser");
    jndiProps.put(Context.SECURITY_CREDENTIALS, "testpassword");
    jndiProps.put("jboss.naming.client.ejb.context", true);
    Context ctx = new InitialContext(jndiProps);
    }

}

Then i got this error

JBREM000200: Remote connection failed: javax.security.sasl.SaslException: 
Authentication failed: all available authentication mechanisms failed - Could 
not register a EJB receiver for connection to remote://localhost:4447  
java.lang.RuntimeException: javax.security.sasl.SaslException: Authentication 
failed: all available authentication mechanisms failed.

Then i added the user using add-user.sh.

enter image description here

Successful Handshake message came.

like image 32
Vinayak Pingale Avatar answered Nov 16 '22 02:11

Vinayak Pingale


you may need add jboss-client.jar and jboss-ejb3-common-client.jar into your library

like image 43
gredezcici Avatar answered Nov 16 '22 01:11

gredezcici