I having file in the location
--src
--> main
--> config
--> application
--> context
--> reference
--> user
--> user.xml
where
--src
--> main
--> config
is in the classpath. Now I am trying to access the file using
Resource resource = new ClassPathResource("classpath**:/application/context/references/user/user.xml");
File file = resource.getFile();
But I getting FileNotFoundException
, I tried with
Resource resource = new ClassPathResource("classpath:/application/context/references/user/user.xml");
File file = resource.getFile();
too, but still I getting the exception. Can someone help me to understand the working of ClassPathResource
and right solution?
ClassPathResource ClassPathResource is a Resource implementation for class path resources. It supports resolution as java. io. File if the class path resource resides in the file system, but not for resources in a JAR.
It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable . jar , some physical file system location used in the classpath (with java 's -cp option), etc.
This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader. getResources(...) call), and then merged to form the final application context definition. So classpath: starts at the root of your classpath.
Use as below
Resource resource = new ClassPathResource("/application/context/references/user/user.xml");
File file = resource.getFile();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With