Read Properties File Outside Jar File

Read properties file outside JAR file

So, you want to treat your .properties file on the same folder as the main/runnable jar as a file rather than as a resource of the main/runnable jar. In that case, my own solution is as follows:

First thing first: your program file architecture shall be like this (assuming your main program is main.jar and its main properties file is main.properties):

./ - the root of your program
|__ main.jar
|__ main.properties

With this architecture, you can modify any property in the main.properties file using any text editor before or while your main.jar is running (depending on the current state of the program) since it is just a text-based file. For example, your main.properties file may contain:

app.version=1.0.0.0
app.name=Hello

So, when you run your main program from its root/base folder, normally you will run it like this:

java -jar ./main.jar

or, straight away:

java -jar main.jar

In your main.jar, you need to create a few utility methods for every property found in your main.properties file; let say the app.version property will have getAppVersion() method as follows:

/**
* Gets the app.version property value from
* the ./main.properties file of the base folder
*
* @return app.version string
* @throws IOException
*/

import java.util.Properties;

public static String getAppVersion() throws IOException{

String versionString = null;

//to load application's properties, we use this class
Properties mainProperties = new Properties();

FileInputStream file;

//the base folder is ./, the root of the main.properties file
String path = "./main.properties";

//load the file handle for main.properties
file = new FileInputStream(path);

//load all the properties from this file
mainProperties.load(file);

//we have loaded the properties, so close the file handle
file.close();

//retrieve the property we are intrested, the app.version
versionString = mainProperties.getProperty("app.version");

return versionString;
}

In any part of the main program that needs the app.version value, we call its method as follows:

String version = null;
try{
version = getAppVersion();
}
catch (IOException ioe){
ioe.printStackTrace();
}

Spring boot how to read properties file outside jar

I'm a bit confused by the title of the question and the description. Hopefully I won't confuse you even more with my comments.

In general, Spring Boot is VERY opiniated about project structure as well as the binary created. The recomended way (Spring Boot opinion) is to build a jar with all dependencies inside (fat jar). If you need configuration properties defined outside your fat jar (or war if that's what you built), Spring Boot offers many options (see reference 1). I like my apps to point to an external file using the flag (spring.config.location) which can be set with a system property:

java -jar -Dspring.config.location=<path-to-file> myBootProject.jar

Notice that you can do something similar by using an environment variable to define where your external file lives.

I hope this helps!

References:
1. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Spring Boot External properties outside of jar

The solution was to delete external.properties and configuration file. And instead of using it put all properties to application.properties. And put application.properties to folder with jar. Spring automatically prioritizes this property file over the property file inside jar.

Accessing prop file that's outside of jar, while running jar

Start app using below and get the flie from location

JAVA_OPTIONS="${JAVA_OPTIONS} -Dexternal.file=/your/location/filedir/filename.txt"

private static File getFile() {
String fileName = System.getProperty("external.file");
return fileName != null ? new File(fileName) : null;
}

Load application.properties outside jar

By convention, Spring Boot looks for an externalized configuration file – application.properties or application.yml – in 4 predetermined locations in the following order of precedence:

  1. /config subdirectory of the current directory
  2. The current directory
  3. classpath /config package
  4. The classpath root

You can place your application.properties in any of the 4 locations without needing to give the location of application.properties while executing the jar. If you want to given any other custom location , then you will have to provide the path of the config location while executing the jar:

java -jar -Dspring.config.location=<path-to-file> myProject.jar

Source: https://www.baeldung.com/spring-properties-file-outside-jar

application.properties outside jar file how to

It doesn't work because you are trying to launch the jar from another folder: spring boot looks for files/folder relative your current folder.

You can:

1) copy application.properties either in ./ or ./config/, relative to your current folder.

2) Or specify -Dspring.config.location:

$ java -Dspring.config.location=target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar

How to read from resource file outside jar

Yes, turn it into a system property, and provide it to anything running any Java process/application.

Let's say you have a config.xml file located inside /some/path/down/the/line/, then you can do: java --classpath ... -Dapp.config=/some/path/down/the/line/config.xml tld.domain.Application.

Then all you have to do in your Java code is to reference that name/path: final String configFile = System.getProperty("app.config");, and use any well-known routine to read it from there.

Basically, you have to make sure the file/path/location is provided somehow to the Java classpath.



Related Topics



Leave a reply



Submit