jboss WildFly中的初始上下文工厂

6za6bjd0  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(137)

我正在使用WildFly 10JDK 8,但一直收到此错误:
严重:无法示例化类:异常错误:无法示例化类:异常类型为“类”。org.jboss.命名.远程.客户端.初始上下文工厂]
我的代码是:

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

正在尝试以main身份运行此类:

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

bnlyeluc1#

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

相关问题