In my project I use the file from the resources/key folder using the ClassLoader and it looks like this
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(folder+"/myfile.json").getFile());
folder in application.properties is written like this key/
Locally, this approach works correctly and I have no problems with it. Problems arise on kubernetes when the path in which the required file is located is written in the folder field and the error says that the file does not exist at this path. As I understand it, Classloader is looking for a file in target and this creates a problem on kubernetes. how to solve this problem?
UPDATE
InputStream fIs = Files.newInputStream(Paths.get(folder+"/myfile.json").toAbsolutePath();
With this approach, it turns out to solve the problem and the file can be found if in Intellij Idea in the working directory write the path to the resources folder. How to solve this problem with Classloader is not yet clear.
In spring boot application how do I give an external windows path using #Value Spring annotation and Resource
The below example works fine that look into resources folder but I want to give the path outside of application like c:\data\sample2.csv
#Value("classPath:/sample2.csv")
private Resource inputResource;
...
#Bean
public FlatFileItemReader<Employee> reader() {
FlatFileItemReader<Employee> itemReader = new FlatFileItemReader<Employee>();
itemReader.setLineMapper(lineMapper());
itemReader.setLinesToSkip(1);
itemReader.setResource(inputResource);
and if I want to get the value from properties file in annotaion, whats the format to put the path in windows?
i tried these, none of them worked:
in code
#Value("${inputfile}")
in properties file:
inputfile="C:\Users\termine\dev\sample2.csv"
inputfile="\\C:\\Users\\termine\\dev\\sample2.csv"
inputfile="C:/Users/termine/dev/sample2.csv"
inputfile="file:\\C:\Users\termine\dev\sample2.csv"
inputfile="file://C://Users//termine///dev//sample2.csv"
When you use classpath spring will try to search with the classpath even if you provide the outside file path.
so instead of using classpath: you can use file:
Ex.
#Value("file:/sample2.csv") //provide full file path if any
Use the key spring.config.location in properties to set the config location. Spring-boot will by default load properties from the locations, with precedence like below :
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
and apart from this when you start the jar or in application.properties you can provide the location of the config file like :
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
You can serve static files from the local disk, by making the resource(s) "sample2.csv" as a static resource. An easy way to do this is by adding spring.resources.static-locations configuration to your applicaiton.properties file. Example:
spring.resources.static-locations=file:///C:/Temp/whatever/path/sample2.csv",classpath:/static-files, classpath:/more-static-resource
When I did this in one of the projects, I was able to access the file form the browser using localhost:8080/sample2.csv.
My Spring Boot project contains an XML file within the src/main/resources folder, which is the common location for such a file.
Running locally and also on Pivotal CloudFoundary, I am able to locate the file and read it in, but on Beanstalk the process results in an empty file.
Code to locate and read file:
URL url = getClass().getResource("/myFile.xml");
LOG.info("File location: " + url.toString());
Resulting log entry:
File location: jar:file:/var/app/current/application.jar!/WEB-INF/classes!/myFile.xml
When I SSH into EC2 instance, I can find the jar in the specified directory.
Do I need to configure Maven to move this file somewhere?
UPDATE
I've since realized that I need to treat this file as in InputStream as it's packaged within the jar.
I'm now using the following code which results in the follow errors:
FileUtils.copyInputStreamToFile(new ClassPathResource("myFile.xml").getInputStream(), myFile);
java.lang.NullPointerException: null
at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:345) ~[commons-io-2.5.jar:2.5]
and
FileUtils.copyInputStreamToFile(new ClassPathResource("classpath:myFile.xml").getInputStream(), myFile);
java.io.FileNotFoundException: class path resource [classpath:myFile.xml] cannot be opened because it does not exist
Thanks!
How does your pom.xml look like? Maybe there is a resource-filter active?
In a spring application you could use File file = ResourceUtils.getFile("classpath:myFile.xml"); to read a resource file.
Could you check the result by using ResourceUtils?
Here the link to the api-documentation:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/ResourceUtils.html#getFile-java.lang.String-
I want to set my relative file path in a properties file so my SaxReader can pick it up when it runs on Tomcat server. I know this should be easy but I've forgotten forgive me :)
I know Spring Boot has application.properties file but I don't see a way to hook in here.
Is there a way to set the relative path in a properties file that will get picked up by Spring Boot and the SaxReader will see it?
As it is I'm hard coding just the filename and putting the file in the resources folder that serves up the templates and static content such as css and js files. The filePath system.out gives: org.dom4j.DocumentException C:sts-bundle\sts-3.7.2.RELEASE\myFileName the toolsuite root location??? weird!!
Please tell me how to specify the relative path in a properties file.
Thanks!!
You can set the file path like any other string property in Spring Boot properties file and access it from inside the path.
E.g. I have the following set in my application.properties file:
download.directory=temp
and it is used as follows in java class:
#Value("${download.directory}")
private String downloadDirectory;
It is used to download the files, now, if I start the application with jar file present in let's say G:/applications folder then the files will be downloaded into G:/applications/temp.
I have a Spring MVC application and a problem with JUnit tests combined with the file applicationContext.xml.
In my JUnit test class I write:
final ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
service = (TestServiceImpl) context.getBean("testServiceImpl");
The error I get is that aplicationContect.xml can not be found:
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
But it exists in the WEB-INF folder.
So, what's wrong here? Why does the file not exist for the JUnit test?
You should keep your Spring files in another folder, marked as "source" (just like "src" or "resources").
WEB-INF is not a source folder, therefore it will not be included in the classpath (i.e. JUnit will not look for anything there).
If you use maven, create a directory called resources in the main directory, and then copy your applicationContext.xml into it.
From your java code call:
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
I got the same error.
I solved it moving the file applicationContext.xmlin a
sub-folder of the srcfolder. e.g:
context = new ClassPathXmlApplicationContext("/com/ejemplo/dao/applicationContext.xml");
The ClassPathXmlApplicationContext isn't going to find the applicationContext.xml in your WEB-INF folder, it's not on the classpath. You could copy the application context into your classpath (could put it under src/test/resources and let Maven copy it over) when running the tests.
I also found this problem. What do did to solve this is to copy/paste this file everywhere and run, one file a time. Finally it compiled and ran successfully, and then delete the unnecessary ones. The correct place in my situation is:
This is under the /src/ path (I am using Intellij Idea as the IDE). The other java source files are under /src/com/package/ path
Hope it helpes.
This happens to me from time to time when using eclipse
For some reason (eclipse bug??) the "excluded" parameter gets a value *.* (build path for my resources folder)
Just change the exclusion to none (see red rectangle vs green rectangle)
I hope this helps someone in the future because it was very frustrating to find.
Click on the src/main/java folder and right click and create the xml file. If you create the application.xml file in a subpackage other than /, it will not work.
Know your structure is look like this
package/
| subpackage/
| Abc.java
| Test.java
/application.xml
enter image description here
I was struggling since a couple of hours for this issue because i was putting that file under resources folder but it didn't help me, finally i realized my mistake.
Put it directly under src/main/java.
For me, it worked by keeping file(applicationContext.xml) in the resources folder
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
I placed the applicationContext.xml in the src/main/java folder and it worked
I solved it moving the file spring-context.xml in a src folder.
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
I fixed it by adding applicationContext.xml into jar/target/test-classes for Maven project. And use
XmlBeanFactory bf = new XmlBeanFactory( new ClassPathResource(
"/applicationContext.xml", getClass() ) )
instead of
XmlBeanFactory bf = new XmlBeanFactory( new ClassPathResource(
"/WEB-INF/applicationContext.xml", getClass() ) )
My solution:
If you have no folder WEB-INF please put the file applicationContext.xml into the folder source(src).
Then Java Project can read file applicationContext.xml -> getBean -> perform your business.
enter image description hereThe solution is to place the xml file in resources folder(src->main-> resources) and use this object creation new ClassPathXmlApplicationContext("applicationContext.xml");
Create a Directory at the bottom of main directory named resources. That solved my issue.
In Spring all source files are inside src/main/java.
Similarly, the resources are generally kept inside src/main/resources.
So keep your spring configuration file inside resources folder.
Make sure you have the ClassPath entry for your files inside src/main/resources as well.
In .classpath check for the following 2 lines. If they are missing add them.
<classpathentry path="src/main/java" kind="src"/>
<classpathentry path="src/main/resources" kind="src" />
So, if you have everything in place the below code should work.
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring-Module.xml");
Please do This code - it worked
AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml");
o/w: Delete main method class and recreate it while recreating please uncheck Inherited abstract method its worked
I'm using Netbeans, i solved my problem by putting the file in: Other Sources default package, then i called it in this way:
ApplicationContext context =new ClassPathXmlApplicationContext("bean.xml");
resources folder
While working with Maven got same issue then I put XML file into src/main/java path and it worked.
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
You actually need to understand the ApplicationContext. It is an interface and it will have different implementations based on configuration.
As you are using new ClassPathXmlApplicationContext("applicationContext.xml"); , kindly pay attention to initial right hand-side , it says ClassPathXmlApplicationContext , so the XML must be present in the class path.
So drag your applicationContext.xml wherever it is to the src folder.
Gist: new ClassPathXmlApplicationContext as the name
[ClassPathXml]will look for the xml file in the src folder of your
project, so drag your xml file there only.
ClassPathXmlApplicationContext—Loads a context definition from an XML
file located in the classpath, treating context definition files as classpath
resources.
FileSystemXmlApplicationContext—Loads a context definition from an XML
file in the file system.
XmlWebApplicationContext—Loads context definitions from an XML file contained
within a web application.
just change the containing package of your applicationContext.xml file.
applicationContext.xml must be in src package not in your project package.
e.g.
src(main package)
com.yourPackageName(package within src)
classes etc.
applicationContext.xml(within src but outside of yourPackage or we can say
parallel to yourPackage name)
your-module/src/applicationContext.xml
val context = ClassPathXmlApplicationContext("applicationContext.xml")
Check the directory path, the default path is /src/ and not /.
GL
I got the same issue while working on a maven project, so I recreate the configuration file spring.xml in src/main/java and it worked for me.
I solved it by moving the file applicationContext.xml in an src folder and main folder.
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");