微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WildFly 中的 InitialContextFactory

如何解决WildFly 中的 InitialContextFactory

我正在使用 WildFly 10JDK 8 并不断收到此错误

严重:无法实例化类:org.jboss.naming.remote.client.InitialContextFactory javax.naming.NoInitialContextException:无法实例化类:org.jboss.naming.remote.client.InitialContextFactory [Root 异常是 java.lang.classNotFoundException:org.jboss.naming.remote.client.InitialContextFactory]

我的代码是:

   public class BuilderFactory {
    
        private static final Logger log = Logger.getLogger(BuilderFactory.class.getName());
    
        // Set up all the default values 
        private static final String INITIAL_CONTEXT_FACTORY = 
        "org.jboss.naming.remote.client.InitialContextFactory";
        private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8085";
    
        public static Context createContext(String userName,String password) {
    
            Context namingContext = null;
            try {
                // Set up the namingContext for the JNDI lookup
                final Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);
                env.put(Context.PROVIDER_URL,System.getProperty(Context.PROVIDER_URL,PROVIDER_URL));
                env.put(Context.Security_PRINCIPAL,userName);
                env.put(Context.Security_CREDENTIALS,password);
                namingContext = new InitialContext(env);
                return namingContext;
            } catch (NamingException e) {
                log.severe(e.getMessage());
                e.printstacktrace();
            }
            return namingContext; }}

尝试将这个类作为主类运行:

    public class SendMessage {
        private static final Logger log = Logger.getLogger(SendMessage.class.getName());
    
        // Set up all the default values
        private static final String DEFAULT_MESSAGE = "Hello,World!";
        private static final String DEFAULT_CONNECTION_FACTORY = 
 "jms/RemoteConnectionFactory";
        private static final String DEFAULT_DESTINATION = "jms/queue/myQueue";
        private static final String DEFAULT_MESSAGE_COUNT = "10";  
        private static final String DEFAULT_USERNAME = "jmsuser";
        private static final String DEFAULT_PASSWORD = "Password1!";      
    
    
        public static void main(String[] args) {
    
            Context namingContext = null;
    
            try {
                String userName = System.getProperty("username",DEFAULT_USERNAME);
                String password = System.getProperty("password",DEFAULT_PASSWORD);
    
                // Set up the namingContext for the JNDI lookup
                namingContext = BuilderFactory.createContext(userName,password);
                
                // Perform the JNDI lookups
                String connectionFactoryString = System.getProperty("connection.factory",DEFAULT_CONNECTION_FACTORY);
                log.info("Attempting to acquire connection factory \"" + 
connectionFactoryString + "\"");
                
                ConnectionFactory connectionFactory = (ConnectionFactory) 
namingContext.lookup(connectionFactoryString);
                log.info("Found connection factory \"" + connectionFactoryString + "\" in 
JNDI");
    
                String destinationString = System.getProperty("destination",DEFAULT_DESTINATION);
                log.info("Attempting to acquire destination \"" + destinationString + "\"");
                
                Destination destination = (Destination) 
namingContext.lookup(destinationString);
                log.info("Found destination \"" + destinationString + "\" in JNDI");
    
                int count = Integer.parseInt(System.getProperty("message.count",DEFAULT_MESSAGE_COUNT));
                String content = System.getProperty("message.content",DEFAULT_MESSAGE);
    
                try ( JMSContext context = connectionFactory.createContext(userName,password) ) {
                    // Send the specified number of messages
                    for (int i = 0; i < count; i++) {
                        context.createProducer().send(destination,content);
                        System.out.println("Sending " + i + " messages with content: " + 
content);
                    }
                }
            } catch (NamingException e) {
                log.severe(e.getMessage());
                e.printstacktrace();
            } finally {
                if (namingContext != null) {
                    try {
                        namingContext.close();
                    } catch (NamingException e) {
                        log.severe(e.getMessage());
                    }}}}}

解决方法

我刚刚找到了更正此错误的解决方案!如果有人遇到此问题,请通过右键单击项目检查路径,选择库,然后在编译时库中添加以下 jar 文件路径:https://i.stack.imgur.com/mHQ3a.png

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。