Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot instantiate InitialContext with JBoss server

Tags:

java

jboss

jndi

I'm trying to create a InitialContext so I could ask the JNDI for some enterprise java beans. The JBoss is running fine, but when I run the java code I get an exception.

I'm running JBoss 7.1

Here is my code:

public class Test {

    public static void main(String[] args){
        InitialContext ctx=getInitialContext();
        Object ref=null;
        try {
            ref = ctx.lookup("ParamEJB/remote");
        } catch (NamingException e) {
            System.out.println("Lookup Failed");
            e.printStackTrace();
        }
        Param stub=(Param)PortableRemoteObject.narrow(ref, Param.class);
        int times=stub.getTimes();
        for(int i=0;i<times;i++)
            System.out.println(stub.getMessage());
    }

    public static InitialContext getInitialContext(){
        Hashtable<String,String> h=new Hashtable<String,String>();
        h.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
        h.put("java.naming.provider.url","localhost");
        try {
            return new InitialContext(h);
        } catch (NamingException e) {
            System.out.println("Cannot generate InitialContext");
            e.printStackTrace();
        }
        return null;
    }
    }

And after i start my JBoss server, I try to run the java code and I get this exception:

javax.naming.NoInitialContextException: Cannot instantiate class:     org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException:     org.jnp.interfaces.NamingContextFactory]
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 client.Test.getInitialContext(Test.java:32)
at client.Test.main(Test.java:13)
Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
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)

What might be the problem?

like image 249
Dani Gilboa Avatar asked Dec 01 '22 02:12

Dani Gilboa


2 Answers

I had the same problem but I found how to fix it. All you have to do is add the jbossall-client.jar library to the clients project, and done!!! You can find the file inside the client folder. e.g jboss-6.1.0.Final_GPT\client I was using Jboss 6.1.0 You can also get help from this link https://community.oracle.com/thread/1157701?start=0

Hope it helps.

like image 161
Thato Pebane Avatar answered Dec 02 '22 15:12

Thato Pebane


The InitialContext properties are not right for the JBoss version that you are using. With JBoss 7, things have changed considerably when you call a ejb from a remote client. This link can help you to instantiate correctly the InitialContex object and to determine the JNDI entry name. Also will tell you what are the necessary dependencies that need to be added to the client classpath.

like image 25
Gabriel Aramburu Avatar answered Dec 02 '22 17:12

Gabriel Aramburu