Access static files in springboot jar - java

I have spring boot application packaged as jar.
The static files I see at /opt/springboot/springboot-app/app.jar!/BOOT-INF/classes!/static/font
How do I access the files under font , but not as an InputStream

Related

Spring boot multi module project: Unable to add yaml property file from dependency module into main application classpath

I am building a fairly simple multi module Spring boot application.
My main module is war, while the dependency module is a simple jar (couple of service classes and yaml property file in src/main/resource folder).
When I deploy my main war application, I don't see Spring boot reading or loading the yaml property file from my dependency jar. I do see it reading application.yaml from my main war application.
In my first attempt, i named both the property files (in War and Jar modules) as application.yaml. I tried renaming the Jar module's yaml file module-application.yaml just to make sure, Spring boot is not overriding the entire file.
What's not working for me is property/config class file in my jar module
Here is a sample class
#PropertySource("classpath*:module-application.yml")
#ConfigurationProperties(prefix="module.orders.urls")
public class SmartApiUrls {
private Log logger = LogFactory.getLog(getClass());
private String saveUrl;
private String getUrl;
private String cancelUrl;
private String barcodesUrl;
---
}
The properties for this config bean are controlled by the dependency jar and this class is defined by Module jar. The main war application doesn't need to know about this. The main application war has it's application.yaml properties
Any help would be great. I am on Spring boot 2.0 and Java 8
Note: Jar module is just a simple service with no main class. It's not executable on it's own. This jar module is a dependency within the main web application. However, jar has it's own yaml property file with name
module-application.yml in it's src/main/resource folder.
Thanks
Sri

Specify spring config file from default config file

I'm building a war package which will be deployed into a web container using spring boot. And I want to put the spring boot config file outside the war file. My idea is to put the file location in System Property and specify it from spring boot's default config file in directory resources, something like this:
spring.config.additional-location: ${encryption.home}/
or
spring.config.location: ${encryption.home}/
But neither works, seems it doesn't load the external config file. How can I do this? Or whether there's another approach for me to put spring boot config file (not in classpath) outside a WAR file.

java web application classpath

I am writing web app using spring. Which is created using standard web application.
I need ApplicationContext to be initialized using ClassPathXmlApplicationContext so i placed context.xml file in WEB-INF folder and created ApplicationContextusing following code.
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
But I get FileNotFoundException saying context.xml is not found.
When i tried to initilize application using FileSystemXmlApplicationContext it works.
But i want ClassPathXmlApplicationContext
what can i do so that context.xml is located in classpath.
You need to place your context.xml inside classes folder or you can use
new ClassPathXmlApplicationContext("classpath:context.xml")
Read the below definition
ClassPathXmlApplicationContext will read files from your classpath. They must be in classes folder of your web application or in a jar in your libfolder.
FileSystemXmlApplicationContext can access all your file system, for example c:/config/applicationContext.xml.
XmlWebApplicationContext certainly can access to files contained in your web application.

Where is the relative path to a packaged Spring Boot application

How/Where can I set the relative path location for a packaged (jar) Spring Boot jar application?
The following is what works in my IDE (IntelliJ).
I have in my application.properties file the following properties.
converter.output=upload-dir/output/
converter.input=upload-dir
I have a Java class that controls the properties for me.
#Component
#ConfigurationProperties("converter")
public class ConverterProperties {
//getters
//setters
}
I have the following directory structure within the IDE.
src/
target/
upload-dir/
upload-dir/output/
pom.xml
README.txt
However, I am wanting to know where my upload-dir and upload-dir/output folders would be when I generate a jar and run it from a folder? I have tried putting the folder in the same location as the jar
C:\app\app.jar
C:\app\upload-dir\
C:\app\upload-dir\output\
But no dice. I setup the #ConfigurationProperties based on this documentation. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html, but I can't seem to find anywhere in there were it talks about packaged jar relative paths.
A typical spring boot application displays some of the information you are looking for in the first line at info level (Starting Application {name} on {host} with PID 1234 ({jarpath} started by {user} in {workdir})
Looking at the source code in StartupInfoLogger, it looks like you need to use the ApplicationHome helper class (in package org.springframework.boot) in order to get that absolute path of the JAR file of your running spring boot application.
Here is an example of Java code to retrieve the location of the jar file and the directory containing the jar file. This is then used to create the uploadDir file (assuming it is a subdirectory of the jar directory)
ApplicationHome home = new ApplicationHome(this.getClass());
File jarFile = home.getSource();
File jarDir = home.getDir();
File uploadDir = new File(jarDir, "upload-dir");
You would want to run this from within one of your application classes running in the spring boot app. Looks like it uses the class passed to the constructor of ApplicationHome in order to find the jar which contains that class.

Cannot read Resource from /static folder in Spring Boot

Given a Spring Boot Maven Project with this structure:
src
main
resources
static
file.txt
This gets packaged into a jar file:
static
file.txt
I have tried the following ways to try to read file.txt:
File file = ResourceUtils.getFile("file.txt");
File file = ResourceUtils.getFile("/file.txt");
File file = ResourceUtils.getFile("static/file.txt");
File file = ResourceUtils.getFile("/static/file.txt");
None of these work.
It's a resource. Packaged inside a jar file. A File represents a path on the file system. So it can't represent an entry of a jar file.
Use MyClass.class.getResourceAsStream("/static/file.txt"). That uses the class loader, and thus loads resources from all the directories and jar files in the classpath.

Categories