Spring can't load xml configuration file - java

This error blows my brain. All the junit tests is fine but when my application loads as plugin for another application I have an errors. The beans.xml is 100% at root of jar file but for some reason the main application, that loads my plugin can't find the file. Why?
In my application's boot method I call
new ClassPathXmlApplicationContext("beans.xml"); throws
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
or
new FileSystemXmlApplicationContext(getClass().getClassLoader().getResource("beans.xml").getPath()); throws
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [file:/C:/path/to/my.jar!/beans.xml]; nested exception is java.io.FileNotFoundException: C:\path\to\my.jar!\beans.xml (The system cannot find the path specified)
UPD: The problem was the application uses custom classloader that does not load resource files in root, META-INF and etc. but only classes

The FileNotFoundException that you get really points out that the beans.xml file is not in your jar at the place where you think it is. I would have a look inside that jar. You can for example open the jar with the 7-zip tool. I find that this provides useful insight when you get errors like the one you report.

If you're getting the FileNotFoundException it is because the file is not located where you're looking for it. The beans.xml file must be located in src/main/resources

Related

RabbitMQ JAVA Spring Configurations - spring.rabbitmq.ssl.key-store

I am trying to connect my Spring application to a RabbitMQ server.
I have in my src/main/resources folder an application.properties file containing the following properties:
spring.rabbitmq.port=port
spring.rabbitmq.username=user
spring.rabbitmq.password=pass
spring.rabbitmq.host=hostname
spring.rabbitmq.ssl.verify-hostname=true
spring.rabbitmq.virtual-host=virtualHost
spring.rabbitmq.ssl.enabled=true
spring.rabbitmq.ssl.algorithm=TLSv1.2
spring.rabbitmq.ssl.key-store=client-file
spring.rabbitmq.ssl.key-store-password=client
The "client-file" is also in the same directory - src/main/resources. However, when I build the application - I recieve the following error:
Factory method 'rabbitConnectionFactory' threw exception;
nested exception is org.springframework.amqp.AmqpIOException: java.io.FileNotFoundException:
class path resource [client-file] cannot be opened because it does not exist
My file is not being recognized... I might need to have some full path, but am not sure what it should look like. Any help would be greatly appreciated!
Try like this:
...
spring.rabbitmq.ssl.key-store=classpath:/client-file
spring.rabbitmq.ssl.key-store-password=client
I did put mine in the resource folder '..\src\main\resources'.
use file:file_path if you are placing the file on a directory location(or a mounted drive) outside your app.

META_INF contents different in jar and war?

I'm trying to use the appassember plugin to create an embedded tomcat using the heroku howto, with tomcat7 and oracle-java-7.
I've used these instructions a number of times to convert a working war file with no problems.
The current project is a spring-batch-admin console with a single runner underneath. I'm not the code owner, and not-so-familiar with spring-batch, but it seems happy to be dropped into a 'normal' /var/lib/tomcat7/webapps.
When launched with the auto-generated shell scripts (sh ./target/bin/webapp), I get the following error:
ERROR - DispatcherServlet - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath*:/META-INF/servlet/*.xml]
Offending resource: URL [jar:file:/vagrant/pairbulkdata_app/downloadjob_batch/target/repo/org/springframework/batch/spring-batch-admin-resources/1.0.0.M1/spring-batch-admin-resources- 1.0.0.M1.jar!/org/springframework/batch/admin/web/resources/servlet-config.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../batch/module-context.xml]
Offending resource: URL [jar:file:/vagrant/pairbulkdata_app/downloadjob_batch/target/repo/org/springframework/batch/pbdDownloadXmlConfig/1.0.0.M1/pbdDownloadXmlConfig-1.0.0.M1.jar!/META-INF/servlet/service-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [jar:file:/vagrant/pairbulkdata_app/downloadjob_batch/target/repo/org/springframework/batch/pbdDownloadXmlConfig/1.0.0.M1/pbdDownloadXmlConfig-1.0.0.M1.jar!/META-INF/servlet/../batch/module-context.xml]; nested exception is java.io.FileNotFoundException: JAR entry META-INF/servlet/../batch/module-context.xml not found in /vagrant/pairbulkdata_app/downloadjob_batch/target/repo/org/springframework/batch/pbdDownloadXmlConfig/1.0.0.M1/pbdDownloadXmlConfig-1.0.0.M1.jar
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
Which seems to imply that it isn't finding the servlet-config.xml file.
The interesting thing, when I go peek into the new myApp.jar vs the old myApp.war files, the contents of the META_INF file have been moved around.
The relevent parts of the original war is:
\META-INF\maven
\WEB-INF\classes\META-INF\stuff-to-launch-the-servlet
In the fat jar, I'm getting:
\META-INF
|-----> maven
|-----> stff-to-launch-the-servlet
and WEB-INF doesn't seem to be baked in at all.
Since the class it can't find is in the stuff that got moved around, I'm heading down that rabbit hole.
Why does the jar file include the META-INF at a different path? The original configuration is on the filesystem, because this isn't a onejar, and should get picked up from the CLASSPATH, correct?

Deployed GWT web application can't find path specified in properties file

I have a properties file placed in glassfish3\glassfish\domains\domain1\config, the application can read the values specified but It can't find the specified path on the same server deployed, ex:
Props.properties content is:
FilePath=//192.xxx.xxx.xxx/glassfish3/FolderName
My app throws an error that says file or folder does not exist even it is there.
What is the proper way to do this?
Welp, I solved it by:
FilePath=/root/glassfish3/FolderName

Eclipse not able to find Spring configuration file

I am wetting my hands in Spring and using Eclipse along with Spring. I have written a very simple Spring application with eclipse to inject a property in a bean. However, when I am running my application, Spring is throwing exception and it seems that the Spring is not able to find the Spring configuration file. Below is the stacktrace --
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.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)
I have tried the following -- Give the full path in the ClassPathXmlApplicationContext method like --
ApplicationContext context = new ClassPathXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");
I have also updated the ClassPath variable in windows to add the path for my spring configuration file. But nothing worked. Any idea would be highly appreciated.
Try this
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:Beans.xml");
And of course your Beans.xml must be in classpath.
Update or maybe
ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/Beans.xml");
Beans.xml should be in classpath. You cannot give full physical path of xml file for ClassPathXmlApplicationContext . Please check if Beans.xml is there in build path of eclipse.
As you are using a full filepath for your Beans.xml example, use
ApplicationContext context = new GenericXmlApplicationContext("C:/Users/devshankhasharm/workspace/FinalPowerShell/src/src/main/Beans.xml");
BUT it is not recommended to do this. Use the ClassPathXmlApplicationContext for this instead.
Then move Beans.xml into the classpath. The simplest way to do this is to move it to the root of your java source if not using Maven or src/main/resources if using Maven
If it's not much of a bother then try using Spring Tool Suite. It's a Spring friendly IDE based on Eclipse, so that you don't have to depend on spring/maven plugin configurations. All you have to do is go and create a Spring Project instead of Java project and rest all the settings will be handled for you.
If you are using Spring Tool Suite (STS), it may be the case that when you create a Maven project the src/main/resources directory was configured with "Excluded: .". In other words, STS sets your src/main/resources directory to have all its contents excluded from output by default.
How to fix it:
Project properties (Alt+Enter) -> Java Build Path -> Source
On src/main/resources, you may see "Excluded: ."
Select this item and click on Edit...
Remove the . exclusion pattern
Click OK. Voila!

Referencing a file in WEbContent folder from an EJB (file not found Exception)

I'm using an XMLStreamWriter to write XML to an RSS file that is located in the WebContent directory.
XMLStreamWriter writer = factory.createXMLStreamWriter(new FileWriter("\\XRSSserverlet\\StatusRSS.rss")
I'm getting a FileNotFound Exception.
What is the best way to write to this file--or any file located in the WeContent Folder. I'm linking to this file from another page that is handled by a servlet as my RSS link.
Thanks.
Full exception: Sever: java.io.FileNoteFoundException: \XRSSservlet\StatusRSS.rss (The system cannot find the path specified)
So I think there is a standard way to reference this folder or I need to add something to a build path somewhere.
Edit: I should add that the calling ejb is in a separate EJB project than the webcontent folder which is in Dynamic Web Project. They are all in the same EAR project though and the build paths are set up correctly.
RSS feed is not static resource I would generate this feed dynamically in servlet as response instead. Even your file path suggests it should be a servlet. Also you could add some caching etc. Just output your XML into response stream and add proper headers.

Categories

Resources