无法实例化InitialContext

时间:2012-04-27 00:51:20

标签: glassfish jms

我有一个类作为Glassfish V3 JMS队列的独立客户端。这个类在我的本地主机上工作正常,即glassfish服务器和独立客户端都在我的本地PC上。

现在我需要在Linux机器上安装此客户端。 Glassfish V3已经在这台Linux机器上运行。我在glassfish安装目录中添加了appserv-rt.jar并将其添加到standlaone客户端的目录中并设置了类路径。但我一直收到这个错误:

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

这是我的Java代码:

  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()) ;
            }
    }

错误来自此行:jndiContext = new InitialContext(props);

如果我使用InitialContext的no-arg构造函数,则没有任何区别。

这是我的unix shell脚本,它调用这个java程序(Standlaone客户端):

  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

请帮助我 - 我长期坚持这个问题。

1 个答案:

答案 0 :(得分:0)

执行which java命令,看看是否正确选取了jdk。我怀疑linux正在挑选gc4j

更新

更改此行 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:

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