XML not exist when it does - java

I am new to Intellij Idea and trying to follow some tutorials. I tried to load an XML into "new ClassPathXmlApplicationContext()". However, there is an error which stated FileNotFoundException.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource [vehicle_beans.xml]; nested exception is java.io.FileNotFoundException:
class path resource [vehicle_beans.xml] cannot be opened because it does not exist
This is my structure:
Here is my code:

XML File location is problem here. As you are using Maven for project, create another folder at level of java named "resources" and put your XML file inside it.
src/main/resources
Resources folder should contain all the config related files to make them available for application to consume. Otherwise you have to pass the full file path to your file reader.

please try this FileSystemXmlApplicationContext instead of ClassPathXmlApplicationContext
private static final ApplicationContext ac =
new FileSystemXmlApplicationContext(
"src/main/java/org/example/vehicle_beans.xml"
);
hope it will work for you

Related

NullPointerException when trying to read a file from resources folder

I would like to read a property file, like this:
Properties props = new Properties();
props.load(MajorBot.class.getResourceAsStream("application.properties"));
But when I am trying to do this I get an error:
Exception in thread "main" java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at java.base/java.util.Properties.load(Properties.java:406)
at majorbot.MajorBot.main(MajorBot.java:13)
My app was created using IntelliJ, new Gradle/Java project. After creation, there was a resources directory allready there. I have created application.properties there, but I cannod read this file.
Is there a way to fix this?
getResourceAsStream will try to find your file in a subfolder of resources that matches your current class’s package. If you want to access a file that is not in a subfolder, specify it as e.g. “/application.properties”.

Spring class path resource [applicationContext.xml] cannot be opened because it does not exist

I'm placing applicationContext.xml in the same directory and package as my Java classes.
Doing the following to read it:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Yet I am consistently met with:
IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException
Which I do not get: The file is there, in the same directory as the classes. They're even compiled to /out and I can see it's all there.
I've tried putting it in src/resources/applicationContext.xml but to no avail.
Spring will look for it in the root of the classpath ...
The "same directory and package as my Java classes" is not at the root of your classpath
src/resources is not on your classpath at all
If you put it in src/main/resources then it will be (1) on your classpath and (2) in the root of your classpath.
I resolved this problem, as changing
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
to
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("packagename/applicationContext.xml");
My XML file also resides in the same package as my java classes exist.

Eclipse Spring Dynamic web project classpath find xml config

I'm new with Spring and Hibernate. I did a project from book Prospring4
But I did not a simple java application, but Dynamic web project (for future). I wrote all code and created a app-context-annotation.xml with beans. In book I have a code to load it:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/app-context-annotation.xml");
In my project I have a standart path - WebContent/META-INF and I add folder with file where. But always I get a error:
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/app-context-annotation.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring/app-context-annotation.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/app-context-annotation.xml] cannot be opened because it does not exist
I read many forum answers but I really don't understand WHY it happens!
I tried to convert my project to Maven project, but got this error again.
PLZ tell me what I'm doing wrong?
EDIT.
Ok. Now this is my project structure, but result is the same exception.
enter image description here
Is the Webcontent folder on your classpath? I mean, when you build your project you see app-context-annotation.xml into the classes folder? If not you should add the Webcontent folder to the build path or move the xml to another folder. p.e: src/main/java

ClassPathXmlApplicationContext error, Spring framework

I have encountered a problem when it comes to the Springs framework, which leads to that the communication between the server and the database does not work.
The project that I created is a Spring project, then refactored to Maven.
At this line in the code:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("projectName/spring.xml");
I get this error:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [projectName/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [projectName/spring.xml] cannot be opened because it does not exist
But it does exist. And I've tried solutions for this problem such as writing ClassPathXmlApplicationContext("spring.xml") instead. This doesn't help however, since then Spring automatically looks in the folder src/main/resources. This doesn't work for me since my project structure doesn't allow me to add this folder and put a XML-file in it. If I try to create this folder, then it is automatically put inside the Java-resources folder, and Eclipse won't allow me to put XML in there.
This is how my project looks:
enter image description here
Is there a way for me to declare where Spring should look for this spring.xml-file?
The ClassPathXmlApplicationContext assumes that the file is on your classpath (Javy describes how to do load a resource from your classpath).
If you want to load the configuration from your file system (as you're doing), you might want to consider using FileSystemXmlApplicationContext instead. Using this mechanism to load your context you can pass a file system location as you're currently doing.
new ClassPathXmlApplicationContext(this.getClass().getResource("/spring.xml").getPath())
try the code above
hope that helped
Spring doesn't look at the src/main/resources, it looks at the classpath.
If you write projectName/spring.xml you need to have this file in bin/projectName/spring.xml or build/projectName/spring.xml. Where bin or build your build folder.
If you build a jar, this file should be in the jar!projectName/spring.xml.
For the web-application this file should be in the WEB-INF/classes/projectName/spring.xml.
If you add src/main/resources at the class path, then content of this folder will be in the build folder. Maven adds src/main/resources at the class path automatically.
Sometimes you should rebuild (clean) your project in the IDE to have such files in the build folder.
Use "FileSystemXmlApplicationContext" as
ApplicationContext context = new FileSystemXmlApplicationContext("spring.xml");

configure classpath in eclipse

How can I configure the classpath so I can use this code without errors
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:appContext/messageSource.xml");
the error I get now is
Exception in thread "main"
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource [messageSource.xml];
nested exception is java.io.FileNotFoundException: class path resource
[messageSource.xml] cannot be opened because it does not exist
Pick Build Path > Use as Source Folder an apply it to the appContext folder. I believe Eclipse adds an entry in the project's .classpath file, which is in your best interest.

Categories