Unable to instantiate class: org.jnp.interfaces.NamingContextFactory

Here is my code:

SpeakerRemote.java

package test;
import javax.ejb.Remote;

@Remote
public interface SpeakerRemote {
    String sayAPhrase( String phrase );
}

SpeakerBean.java

package test;
import javax.ejb.Stateless;

@Stateless
public class SpeakerBean implements SpeakerRemote {
    @Override
    public String sayAPhrase( String phrase ){
         return "Speaker Service:\t" + phrase;
    }
}

I put it together with maven. Here I will show you the link to the part:

Invoker.java

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

public class Invoker {
    public static InitialContext getContext() throws NamingException {
        final Properties properties = new Properties();
        properties.put( Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory" );
        properties.put( Context.PROVIDER_URL, "jnp://127.0.0.1:1099" );
        return new InitialContext( properties );
    }
}

Main.java

import test.SpeakerRemote;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {
    public static void main( String... args ) {
        try {
            InitialContext context = Invoker.getContext();
            SpeakerRemote speaker = ( SpeakerRemote ) context.lookup( "SpeakerBean/remote" );
            System.out.println( speaker.sayAPhrase( "Hello, World!" ) );
        }
        catch ( NamingException e ) {
            e.printStackTrace();
        }
    }
}

By running this application, I got this exception:

"Unable to instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception - java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]"

Please help me because I really need to understand this!

PS I am using jboss 7.1.1 Final + EJB 3.1 + Maven 3.1.1 + Java 1.7 + Win7

+3
source share
2 answers

Read this documentation for JBoss 7.1

EJB API- JNDI-EJB

classpath jboss-client.jar $JBOSS_HOME/bin/client

Invoker.java

Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.PROVIDER_URL,"remote://127.0.0.1:4447");
// username
properties.put(Context.SECURITY_PRINCIPAL, "user");
// password
properties.put(Context.SECURITY_CREDENTIALS, "password");
// This is an important property to set if you want to do EJB invocations via the remote-naming project
properties.put("jboss.naming.client.ejb.context", true);
// create a context passing these properties
return new InitialContext(properties);

Main.java

// lookup the bean    
SpeakerRemote speaker = (SpeakerRemote) context.lookup("myapp/myejbmodule/SpeakerBean!test.SpeakerRemote);
System.out.println( speaker.sayAPhrase( "Hello, World!" ) )

: EJB JNDI

classpath jboss-ejb-client.properties

JBoss--EJB

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
#if you test on local machine
remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=JBOSS-LOCAL-USER

remote.connections=default
remote.connection.default.host=127.0.0.1
remote.connection.default.port = 4447
remote.connection.default.username=user
remote.connection.default.password=password

Invoker.java

final Hashtable<String, String> props = new Hashtable<String, String>();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
return new InitialContext(props);

Main.java

SpeakerRemote speaker = (SpeakerRemote) context.lookup("ejb:myapp/myejbmodule/SpeakerBean!test.SpeakerRemote);
System.out.println( speaker.sayAPhrase( "Hello, World!" ) )
+3

package test;
import javax.ejb.Stateless;
import org.jboss.ejb3.annotation.RemoteBinding;  

@Stateless
@RemoteBinding(jndiBinding="SpeakerBean/remote")
public class SpeakerBean implements SpeakerRemote {
@Override
 public String sayAPhrase( String phrase ){
     return "Speaker Service:\t" + phrase;
}
}
0

Source: https://habr.com/ru/post/1772920/


All Articles