I have a Spring Boot App. I am trying to read few files that I have placed under main/resources folder. I see that Spring Boot automatically reads application.properties under resources folder. However it doesn't seem to read other xml / text files that I have placed under resources folder.
Source xslt = new StreamSource(new File("removeNs.xslt"));
Should I add any additional configuration for the program to automatically read all the files under resources folder without having to explicitly provide the path?
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("removeNs.xslt").getFile());
This should work.
Thanks #Edwin for mentioning a more robust solution:
File file = new File(Thread.currentThread().getContextClassLoader().getResource("removeNs.xslt").getFile());
You need to specify that you want to read specific files under resources folder using
#PropertySource annotation.
you can specify multiple property sources using
#PropertySources({
#PropertySource(),
#PropertySource()
})
Related
I am developping Spring Java application with Maven. I need to read a .json file, that is situated in Resources folder in my project, and than return it by Controller again as a .json file. How can I do that? I am a beginner...
Resourece folder is in your class path. Any file which is in your class path , you can read through this.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourFile.json");
you can read by using getResourceAsStream.
If you access to file in Spring bean you could also use #Value annotation like that (It work only in spring beans)
#Value("classpath:data/resource-data.txt")
Resource resourceFile;
https://www.baeldung.com/spring-classpath-file-access#2-using-value
I want to load a html file located in:
to use in standalone spring boot jar application.
This approach leads to FileSystemNotFoundException
new InputStreamReader(
getClass().getResourceAsStream("/email-templates/html-email.html")
)
Using
#Autowired
private ResourceLoader resourceLoader;
...
resourceLoader.getResource("classpath:email-templates/html-email.html");
leads to NullPointerException.
Could you please specify how to properly load a file in spring boot jar.
Try below code. Using spring classpathresource you should be able to load them. As long as you try load this file inside spring context, Spring container is aware of its classpath .So it should load corresponding file.Otherwise , it tries to find in file system.
Resource resource = new ClassPathResource("email-templates/html-email.html");
I assume you have these resouces in your jar, Check inside the jar file if those resources are present.
jar tf springboot.jar
Load resource in a jar file Java Spring
Try through below code.
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("email-templates/html-email.html");
/email-templates/html-email.html
If the above doesn't work visit this help full link
Java ClassLoaderUtil
I'm using the log4j2 library to manage the logging process.
I created a configuration file named log4j2.xml containing the Appenders and Loggers configurations. Then, I defined a Logger in each class
private static Logger my_logger = LogManager.getLogger(my_class);
I did not specify anywhere the name of the conf file, so I think that the library implicitly get and read it.
Now, I need to provide my application in the form of a jar file, so I need to make the config file available so that the user can modify and configure it.
In my case, I suggest to create a XXX folder at the level of the jar file, containing all the configuration files used by my app.
My question is how can I say to the app "get XXX/log4j2.xml" rather than the xml contained into the jar.
that config file must be located in the class path, if you want the app to read the configuration from any other location then you need to specify that using
PropertyConfigurator.configure("/myPath/log4j.properties");
Make any folder and put your property or xml file in that. In order to read the property file you can do something like this:
Properties objProperties = new Properties();
<your-class>.class.getClassLoader().getResource("folder/log4j.properties");
objProperties.load(isFile);
or, Also this:
InputStream ist = Thread.currentThread().getContextClassLoader().getResourceAsStream("folder/log4j.properties");
In case of java web application please use the link
I had a similar task a few weeks ago.
I solved it this way:
Store a template of your log4j2.xml inside your jar files resource folder
When running your application, check for a file named log4j2.xml in the jar files current directory
If there is one, use that to create your logger
If not, copy your template from within your jar to the jar files directory and then use that to create your logger.
Cheers
I manually inject a properties file inside a jar.
How to load properties from a jar file before java 1.7 ?
I tried many workarounds and nothing worked so far.
There's plenty questions about it, but everything is focused on ClassLoader methods from java 1.7.
When you have a properties file inside your classpath or inside your jar file it becomes a resource. Any other case is a simple file.
What you need to do, before you package your jar file, is add to your classpath the folder where the properties files are (i.e myproject/src/main/resources/) then wherever you do a
Properties properties = new Properties();
properties.load(MyClass.class.getResourceAsStream("/yourPropsFileName"));
it will load it!
Although, if you are using an external property file you can also load it by using:
Properties properties = new Properties();
properties.load(new FileInputStream("extenalPropsFileLocation"));
Hope it helps!
From some class, call:
getClass().getResourceAsStream("/path/to/props.props")
Make sure that the path matches up with a classpath location.
I am creating an executable JAR that uses a couple XML config files, one for the application and one for log4j. To reference my app config file, I do this:
InputStream config = Util.class.getResourceAsStream("/config/config.xml");
This works fine for my app config, but the problem is that I can't configure log4j like this. Here is the code that configures log4j:
DOMConfigurator.configure("/config/log4j.xml");
This won't work because the XML file is going to be stored within the packaged JAR. How can I configure log4j to use an XML or properties file within the JAR?
You can use the URL version of the DOMConfigurator.configure method. The resource will have to be available at/config/log4j.xml.
DOMConfigurator.configure(Util.class.getResource("/config/log4j.xml")
You can try
DOMConfigurator.configure(Util.class.getResource("/config/log4j.xml"));