-1

Following is my directory structure

/app/infra/myjar/bin --> contains my runnable jar
/app/infra/myjar/config --> contains all external config files which I want to read from code which is inside jar.

Following is my code to read the external spring config file

ApplicationContext basicConfig =  new ClassPathXmlApplicationContext("classpath:/spring-bean-mapping.xml");

My MANIFEST file has following entry.

Class-Path:
.
../config/

Also I have set the CLASSPATH variable in my .bashrc to point to /app/infra/myjar/config folder

When I am trying to run the jar using

java -jar myjar.jar

I am getting following exception

Loading Spring Bean Mapping file
log4j:WARN No appenders could be found for logger        (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-bean-mapping.xml]; nested exception is      java.io.FileNotFoundException: class path resource [spring-bean-mapping.xml] cannot be opened because it does not exist
    at  org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.vzw.esper.alerts.consumer.EsperMessagesConsumer.main(EsperMessagesConsumer.java:47)
Caused by: java.io.FileNotFoundException: class path resource [spring-bean-mapping.xml]  cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 13 more
Exception in thread "main" java.lang.NullPointerException
    at com.vzw.esper.alerts.consumer.EsperMessagesConsumer.main(EsperMessagesConsumer.java:86)

How can I understand what is going wrong here?

halfer
  • 19,824
  • 17
  • 99
  • 186
Shantanoo K
  • 765
  • 5
  • 15
  • 43

2 Answers2

0

Fortunately I was able to resolve the above mentioned issue. The issue was when I was creating a Executable jar through Eclipse it was overriding my MANIFEST.MF class-path entries.

So after the jar has created I changed the MANIFEST.MF file to have desired entries and it worked.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shantanoo K
  • 765
  • 5
  • 15
  • 43
  • (Note: if you get a partial solution, then it's fine to give that as an answer, but please do not ask additional questions in answers. They should be asked in a new question). – halfer Aug 18 '18 at 16:16
0

I was able to resolve the 2nd issue where I was getting issue with spring component scanning.

I followed below spring forum link and it worked!!!

http://forum.spring.io/forum/spring-projects/container/89556-component-scanning-works-in-ide-but-not-in-an-executable-jar-file

Shantanoo K
  • 765
  • 5
  • 15
  • 43