IT TIP

NoInitialContextException 오류의 의미

itqueen 2020. 12. 8. 20:35
반응형

NoInitialContextException 오류의 의미


내 EJB에 대한 클라이언트를 작성 중이며 실행하려고 할 때 다음 예외가 발생합니다.

javax.naming.NoInitialContextException: 환경이나 시스템 속성, 애플릿 매개 변수 또는 응용 프로그램 리소스 파일에서 클래스 이름을 지정해야합니다.

문제가 무엇인지 이해할 수 없습니다.


javax.naming패키지는 JNDI API를 포함한다. 구현이 아닌 API 일 뿐이므로 사용할 JNDI 구현을 지정해야합니다. 구현은 일반적으로 통신하려는 서버에 따라 다릅니다.

구현을 지정하려면, 당신은 전달 Properties당신이를 구성 할 때 객체 InitialContext. 이러한 속성은 사용할 구현과 서버 위치를 지정합니다. 기본 InitialContext생성자는 시스템 속성이있는 경우에만 유용하지만 속성은 수동으로 전달한 것과 동일합니다.

설정해야하는 속성은 서버에 따라 다릅니다. 이러한 설정을 찾아서 연결해야합니다.


jndi.properties를 설정해야합니다. activemq에 대해 속성이 설정되는 방법을 설명하는 코드를 아래에 제공했습니다. 그렇게 당신은 당신의 응용 프로그램에 설정할 수 있습니다. JBoss와 같은 J2EE 컨테이너 내부에서는 이러한 속성을 설정할 필요가 없습니다.

Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
InitialContext ctx = new InitialContext(props);
// get the initial context
// InitialContext ctx = new InitialContext();
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");        
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();   
queueConn.start();
// lookup the queue object
Queue queue = (Queue) ctx.lookup("dynamicQueues/Payment_Check");  

나는 이것이 늦은 대답이라는 것을 알고 있지만 향후 참조를 위해 제공하는 것입니다.


다음 이름 / 값 쌍을 해시 테이블에 넣고이 생성자를 호출해야합니다.

public InitialContext(Hashtable<?,?> environment)

정확한 값은 애플리케이션 서버에 따라 다릅니다.이 예제는 jboss 용입니다.

jndi.java.naming.provider.url=jnp://localhost:1099/
jndi.java.naming.factory.url=org.jboss.naming:org.jnp.interfaces
jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

JNDI 문제입니다. InitialContext클래스에 JNDI 서비스 공급자에 대한 기본 속성이나 명시 적으로 구성된 서버 속성이없는 경우 해당 예외가 표시 됩니다.

Context.INITIAL_CONTEXT_FACTORY환경 특성을 사용중인 초기 컨텍스트 구현의 클래스 이름으로 설정하십시오 . 이 클래스는 클래스 경로의 프로그램에서 사용할 수 있어야합니다.

검사:


특히 InitialContextSpringBoot에서 임베디드 Tomcat7 인스턴스 내 에서 기본값 (인수 없음)을 검색하려고 할 때이 문제가 발생했습니다 .

나를위한 해결책은 Tomcat에게 enableNaming.

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}

쉽고 구성 가능한 솔루션은 하나의 jndi.properties 파일을 만들고이 파일을 클래스 경로에 넣는 것입니다. jndi.properties는 다음과 같이 만들 수 있습니다.

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector
java.naming.provider.url = vm://localhost

# use the following property to specify the JNDI name the connection factory
# should appear as. 
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue


# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic

이름 지정 팩토리 및 URL을 지정하고이 파일을 클래스 경로에 넣으십시오. JMS는 필요한 정보를 자체적으로 가져 오며 향후에도 쉽게 구성 할 수 있습니다.


대부분의 경우 이러한 설정은 jndi.properties파일 에도 정의 됩니다. 저거 어딘가에 누워 있나요?


내 프로젝트에 다음 Jar 라이브러리를 추가하여 동일한 문제를 해결했습니다.

  • appserv-rt.jar
  • javaee.jar

폴더에서 : C:\Program Files\glassfish-4.0\glassfish\lib

The links to these libraries were broken and Netbeans didn't found the right classes to use.


My problem with this one was that I was creating a hibernate session, but had the JNDI settings for my database instance wrong because of a classpath problem. Just FYI...


you need to use jboss-client.jar in your client project and you need to use jnp-client jar in your ejb project


make sure dependencies for jetty naming and jetty plus are included (not just provided scope). This fixed it for me.


Do this:

Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
Context initialContext = new InitialContext(props);

Also add this to the libraries of the project:

C:\installs\glassfish\glassfish-4.1\glassfish\lib\gf-client.jar adjust path accordingly

참고URL : https://stackoverflow.com/questions/1525385/the-meaning-of-noinitialcontextexception-error

반응형