I have a static main that points to an application Context file. Before the project was made a maven project, I kept "applicationContex.xml" in the "" (i.e. root) of Netbeans. Now when I run my code I get the error shown.
Can someone tell me the right location for my application context file?
#Service
public class TestDriver {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Error:
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
Directory of C:\NetBeansProjects\prj\trunk\src\main\java
8/15/2012 09:38 AM <DIR> .
8/15/2012 08:58 AM <DIR> ..
8/15/2012 09:38 AM 162 jcbc.properties
8/15/2012 09:38 AM 879 log4j.properties
8/15/2012 09:38 AM 1,691 applicationContext.xml
3 File(s) 2,732 bytes
Following the maven conventions keep the xml files under src/main/resources
From SpringSource
src/main/java
Contains the Java source code for your project
src/main/resources
Contains any classpath-relative resources for your project (like, a Spring application context .xml file)
src/test/java
Contains the java source code for your test classes. This directory will not be included in the final build. All tests herein will be compiled and all tests will be run. If the tests fail, it aborts the build.
src/test/resources
This directory will not be included in the final Java build. This folder contains any classpath-relative resources for your test code (like, a Spring application context .xml file).
You need to put the non-java files in:
C:\NetBeansProjects\prj\trunk\src\main\resources
Related
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.
I have a runnable jar file which is not able to access my resources which reside outside of the default src directory. Based on my understanding from What is the difference between Class.getResource() and ClassLoader.getResource(), I should be able to access root/res/img/img1.png (see folder setup below) by using the following getResourceFile function:
public class Foo {
private static final ClassLoader CLASS_LOADER = Foo.class.getClassLoader();
public static File getResourceFile(String relativePath) {
// Since I'm using getClassLoader, the path will resolve starting from
// the root of the classpath and it'll take an absolute resource name
// usage: getResourceFile("img/img1.png")
// result: Exception in thread "main" java.lang.NullPointerException
return new File(CLASS_LOADER.getResource(relativePath).getFile());
}
}
folder setup:
root/
src/
foo/
bar/
res/
img/
img1.png
audio/
audio1.wav
The problem arises when I try to execute the jar executable itself. However, the strange thing is that I was not able to replicate this through eclipse IDE which was actually able to resolve the path correctly. I have added the resource directory to the build path via (Project -> Properties -> Java Build Path -> Add Folder) so Java should be able to find the resource folder at runtime.
Is there something I'm missing in terms of generating the jar file? When unpacking the jar file everything seems to be in order with the img and audio directories being in the root (given the above initial folder setup):
foo/
/bar
img/
img1.png
audio/
audio1.wav
Files can only be used to represent actual files in your filesystem. And once you package your files into a JAR, the resource (img/img1.png) is not a file anymore, but an entry in the JAR file. As long as you use the folder structure from within Eclipse, the resources are individual files so everything is fine.
Try this:
System.out.println(CLASS_LOADER.getResource(relativePath));
It will print a URL, but it will not be a valid path to a file in your file system, but to an entry within the JAR file.
Usually, you will only want to read a resource. In that case, use getResourceAsStream() to open an InputStream.
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
I have an application loading its Spring context with XML configuration.
Context files are stored in a jar's classpath (in the META-INF/context directory).
Introducing a new bean, I want to load a beanio mapping file from within the classpath of the same archive.
With Tomcat running within Eclipse it all works as a charm, but when I deploy a WAR file to our QA environment I get the application not starting.
Let me walk the code:
public class BeanIoParser implements Parser, Iterator<Message>, InitializingBean
{
private StreamFactory streamFactory;
private Resource resourceMapping; //Injected via Context
#Override
public void afterPropertiesSet() throws Exception
{
streamFactory = StreamFactory.newInstance();
streamFactory.load(resourceMapping.getFile()); //boom here
}
}
And then the context
<bean id="messagesParser" class="it.acme.BeanIoParser" scope="prototype">
<property name="resourceMapping" value="classpath:META-INF/mappings/messages-beanio.xml" />
</bean>
And then what? Uhm... I have double checked the built jar file by uncompressing the war file first. It all matches.
I can see my messages-beanio.xml file correctly.
But when I start the application I get the following root cause:
java.io.FileNotFoundException: class path resource [META-INF/mappings/messages-beanio.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%208.0/temp/11-app/WEB-INF/lib/app-3.0.0.20151105.jar!/META-INF/mappings/messages-beanio.xml
I have also tried another way, by using classpath* (because we already faced this problem when loading Spring context from multiple jar files)
So if my bean declaration becomes
<property name="resourceMapping" value="classpath*:META-INF/mappings/messages-beanio.xml" />
My error becomes
java.io.FileNotFoundException: ServletContext resource [/classpath*:META-INF/mappings/messages-beanio.xml] cannot be resolved to absolute file path - web application archive not expanded?
How can I fix?
Solved differently. I programmatically instantiate the ClasspathResource with path starting with META-INF and not classpath:
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.