[java] java.io.FileNotFoundException: class path resource cannot be opened because it does not exist

I am trying to set the configuration location for my Project but I keep getting the following error:

java.io.FileNotFoundException: class path resource [main/resources/app-context.xml] cannot be opened because it does not exist

I have my project set up like this:

enter image description here

And I have my code set up as:

ApplicationContext context = new ClassPathXmlApplicationContext(configLocation: "main/resources/app-context.xml");

How can I fix this?

This question is related to java spring file intellij-14

The answer is


The file location/path has to relative to your classpath locations. If resources directory is in your classpath you just need "app-context.xml" as file location.


We can also try this solution

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:app-context.xml");

in this the spring automatically finds the class in the class path itself


Try this:

ApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to intellij-14

java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Error: Unfortunately you can't have non-Gradle Java modules and > Android-Gradle modules in one project