Cannot Instantiate InitialContext

2019-08-30 19:17发布

问题:

I have a class that acts as standalone client for Glassfish V3 JMS queue. This class works fine from my localhost, i.e. both glassfish server and the standalone client are on my local PC.

Now I need to install this client on a Linux machine. Glassfish V3 is already running on this Linux machine. I have added appserv-rt.jar from the glassfish installation directory and added it in the directory of standlaone client and set the classpath. But I keep getting this error:

javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.init(InitialContext.java:223)
        at javax.naming.InitialContext.<init>(InitialContext.java:197)
        at com.cisco.zbl.controller.ZblBulkUploadThread.run(ZblBulkUploadThread.java:55)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
        ... 5 more

Here is my Java code:

  public class ZblBulkUploadThread implements Runnable,MessageListener{

    private static final Category log = Category.getInstance(ZblBulkUploadThread.class) ;


            private Queue queue;

    public void run()
    {
            try
            {
                    ZblConfig zblConfig = new ZblConfig() ;


                    InitialContext jndiContext = null;

                    MessageConsumer messageConsumer=null;

                    Properties props = new Properties();
            props.setProperty("java.naming.factory.initial",  "com.sun.enterprise.naming.SerialInitContextFactory");
            props.setProperty("java.naming.factory.url.pkgs",  "com.sun.enterprise.naming");
            props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

                    jndiContext = new InitialContext(props);

                    log.debug(zblConfig.getProperty("JMSConnectionFactoryName")) ;

                    //System.setProperty("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");

                    ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.lookup(zblConfig.getProperty("JMSConnectionFactoryName"));


                    Connection connection = connectionFactory.createConnection();


                    Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

                    queue = (Queue)jndiContext.lookup(zblConfig.getProperty("JMSQueueName")) ;

                    messageConsumer = session.createConsumer(queue);

                    connection.start();

                    while(true)
                    {

                            Message message = messageConsumer.receive() ;


                            ObjectMessage om = ((ObjectMessage)message) ;
                            try
                            {

                                    RedirectFile file = (RedirectFile)om.getObject() ;

                                    log.debug("filePath "+file.getFilePath()) ;
                                    log.debug(" userName "+file.getUserName()) ;
                                    log.debug(" mode is "+file.getMode()) ;

                                    processMessage(file,zblConfig) ;

                            }
                            catch(Exception ex)
                            {
                                    log.error("ERROR "+ex.getMessage()) ;
                                    ex.printStackTrace() ;
                            }
                    }


            }
            catch(Exception ex)
            {

                    ex.printStackTrace() ;
                    log.error("Error "+ex.getMessage()) ;
            }
    }

The error comes at this line: jndiContext = new InitialContext(props);

It does not make any difference if I use the no-arg constructor of InitialContext.

Here is my unix shell script that invokes this java program (Standlaone client):

  APP_HOME=/local/scripts/apps/bulkUpload;

CLASSPATH=.:$APP_HOME/lib/gf-client.jar:$APP_HOME/lib/zbl.jar:$APP_HOME/lib/log4j-   1.2.4.jar:$APP_HOME/lib/javaee.jar:$APP_HOME/lib/poi-3.8-beta5-20111217.jar:$APP_HOME/lib/poi-examples-3.8-beta5-20111217:$APP_HOME/lib/poi-excelant-3.8-beta5-20111217:$APP_HOME/lib/poi-ooxml-3.8-beta5-20111217:$APP_HOME/lib/poi-ooxml-schemas-3.8-beta5-20111217:$APP_HOME/lib/poi-scratchpad-3.8-beta5-20111217:$APP_HOME/lib/appserv-rt.jar:


 echo "CLASSPATH=$CLASSPATH";

 export APP_HOME;
 export CLASSPATH;

 cd $APP_HOME;

 #javac -d . ZblBulkUploadThread.java

 java -cp $CLASSPATH -Dzbl.properties=zbl-stage.properties    -Djava.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory com.cisco.zbl.controller.ZblBulkUploadThread

Please help me - I have been stuck on this problem for a long time.

回答1:

do a which java command and see if jdk is being picked up correctly. I doubt that linux is picking gc4j

update:

change this line CLASSPATH=.:$APP_HOME/lib/gf-client.jar:$APP_HOME/lib/zbl.jar:$APP_HOME/lib/log4j- 1.2.4.jar:$APP_HOME/lib/javaee.jar:$APP_HOME/lib/poi-3.8-beta5-20111217.jar:$APP_HOME/lib/poi-examples-3.8-beta5-20111217:$APP_HOME/lib/poi-excelant-3.8-beta5-20111217:$APP_HOME/lib/poi-ooxml-3.8-beta5-20111217:$APP_HOME/lib/poi-ooxml-schemas-3.8-beta5-20111217:$APP_HOME/lib/poi-scratchpad-3.8-beta5-20111217:$APP_HOME/lib/appserv-rt.jar: to

CLASSPATH=.:$APP_HOME/lib/gf-client.jar:$APP_HOME/lib/zbl.jar:$APP_HOME/lib/log4j-   1.2.4.jar:$APP_HOME/lib/javaee.jar:$APP_HOME/lib/poi-3.8-beta5-20111217.jar:$APP_HOME/lib/poi-examples-3.8-beta5-20111217:$APP_HOME/lib/poi-excelant-3.8-beta5-20111217:$APP_HOME/lib/poi-ooxml-3.8-beta5-20111217:$APP_HOME/lib/poi-ooxml-schemas-3.8-beta5-20111217:$APP_HOME/lib/poi-scratchpad-3.8-beta5-20111217:$APP_HOME/lib/appserv-rt.jar


标签: glassfish jms