I am using WildFly 10, JDK 8 and keep getting this error:
SEVERE: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
My code is:
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; }}
Trying to run this class as 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());
}}}}}
I've just found the solution to correct this error! In case anyone runs into this issue, check the path by right click on the project, select Libraries and at the Compile-time Libraries add the following jar file path: https://i.stack.imgur.com/mHQ3a.png