我正在使用WildFly 10、JDK 8并不断收到此错误:
严重:无法实例化类:org.jboss.naming.remote.client.InitialContextFactory javax.naming.NoInitialContextException:无法实例化类:org.jboss.naming.remote.client.InitialContextFactory [根异常是 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());
}}}}}