Java load properties resources

Loading Java Properties Files

The Class.getResourceAsStream(name) returns an Inputstream for a resource with a given name or null if no resource with the given name is found. The name of a resource is a ‘/’-seperated path name that identifies the resource. If the name with a leading “/” indicates the absolute name of the resource is the portion of the name following the ‘/’. In Class.getResourceAsStream(name), the rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object’s class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String). So in our example to load config.properties we will have a method loadProps1() .

private Properties configProp = new Properties(); . public void loadProps1() < InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >>
Code language: Java (java)
this.getClass() .getClassLoader() .getResourceAsStream("some/package/config.properties");
Code language: Java (java)

The ClassLoader.getResourceAsStream(name) returns an Inputstream for reading the specified resource or null if the resource could not be found. The name of a resource is a ‘/’-seperated path name that identifies the resource. The name no leading ‘/’ (all namea are absolute). So in our example to load config.properties we will have a method loadProps2() .

private Properties configProp = new Properties(); public void loadProps2() < InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >>
Code language: Java (java)

java-load-properties

The folder structure of our example code will be: The full Java code for testing

package net.viralpatel.java; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.ResourceBundle; public class LoadPropertiesExample < private Properties configProp = new Properties(); public static void main(String[] args) < LoadPropertiesExample sample = new LoadPropertiesExample(); sample.loadProps2(); sample.sayHello(); > public void loadProps1() < InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >> public void loadProps2() < InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >> public void sayHello() < System.out.println(configProp.getProperty("hello.world")); > >
Code language: Java (java)

Источник

Loading Java Properties Files

The Class.getResourceAsStream(name) returns an Inputstream for a resource with a given name or null if no resource with the given name is found. The name of a resource is a ‘/’-seperated path name that identifies the resource. If the name with a leading “/” indicates the absolute name of the resource is the portion of the name following the ‘/’. In Class.getResourceAsStream(name), the rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object’s class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String). So in our example to load config.properties we will have a method loadProps1() .

private Properties configProp = new Properties(); . public void loadProps1() < InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >>
Code language: Java (java)
this.getClass() .getClassLoader() .getResourceAsStream("some/package/config.properties");
Code language: Java (java)

The ClassLoader.getResourceAsStream(name) returns an Inputstream for reading the specified resource or null if the resource could not be found. The name of a resource is a ‘/’-seperated path name that identifies the resource. The name no leading ‘/’ (all namea are absolute). So in our example to load config.properties we will have a method loadProps2() .

private Properties configProp = new Properties(); public void loadProps2() < InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >>
Code language: Java (java)

java-load-properties

The folder structure of our example code will be: The full Java code for testing

package net.viralpatel.java; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.ResourceBundle; public class LoadPropertiesExample < private Properties configProp = new Properties(); public static void main(String[] args) < LoadPropertiesExample sample = new LoadPropertiesExample(); sample.loadProps2(); sample.sayHello(); > public void loadProps1() < InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >> public void loadProps2() < InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties"); try < configProp.load(in); >catch (IOException e) < e.printStackTrace(); >> public void sayHello() < System.out.println(configProp.getProperty("hello.world")); > >
Code language: Java (java)

Источник

Читайте также:  Do ipk knoc ru login index php

Loading a properties file from the resources folder in Java could be the rephrased

To load the properties file, it must be placed in the directory for successful loading. Various frameworks offer distinct class loaders to achieve this.

Cannot load properties file from resources directory

Once compiled, if the file is located in target/classes, it is already included in the build path. The default directory for resources like this is src/main/resources in Maven, and the Eclipse Maven plugin (M2E) automatically adds it to the build path. Therefore, there’s no need to relocate your properties file.

The second subject pertains to obtaining resources. Resources that are included in the build path will automatically be included in the class path when the Java program is running. Therefore, it is recommended to use a class loader to load these resources. See the code example below:

String resourceName = "myconf.properties"; // could also be a constant ClassLoader loader = Thread.currentThread().getContextClassLoader(); Properties props = new Properties(); try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) < props.load(resourceStream); >// use props here . 

You should place it within the tag src/main/resources and then proceed to load it using the following method:

props.load(new FileInputStream("src/main/resources/myconf.properties")); 

To load it, your method of loading will initially search in the base folder of your project. If the file is located in target/classes and you wish to load it from that location, follow these instructions:

props.load(new FileInputStream("target/classes/myconf.properties")); 

In case of a basic application, the method of «getSystemResourceAsStream» may also be applicable.

try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties")).. 

Load file from resources folder java Code Example, File file = new File( getClass().getClassLoader().getResource(«database.properties»).getFile() );

Читайте также:  Python print with comma

Java: how can I load .properties from resources folder?

Various frameworks offer distinct class loaders, and the one you possess is likely the «default» class loader that adheres to the JDK documentation. While you may come across this page — http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String), the more beneficial page is — http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String), which specifies that you can initiate your resource name with a forward slash.

Although the rules have been documented, it is disheartening to note that your class loader may not comply with them. As a result, there are three options available to explore.

  1. absolute starting with a slash.
  2. Unconditional, lacking a beginning forward slash.
  3. relative without any slashes.

Apart from the three variations, it’s crucial to invoke the getResourceAsStream() method on the appropriate class. The class should be present in the same jar file that contains your properties file. This can be perplexing, but fortunately, you began with a basic illustration. 🙂

public static void main(String[] args) < new NewMain().doit(); >public void doit() < Properties prop = new Properties(); try (InputStream inputStream = NewMain.class.getResourceAsStream("/data/newproperties.properties")) < prop.load(inputStream); System.out.println("hello " + prop.getProperty("x", "default")); >catch (FileNotFoundException e) < e.printStackTrace(System.out); >catch (IOException e) < e.printStackTrace(System.out); >> 

If you’re not using the standard directory layout, make sure you have a good reason for doing so. Otherwise, I recommend that you switch to it. Currently, the /resources folder is not recognized as a classpath folder, as evidenced by the fact that /target/classes/sb/elements/SBMessages.properties does not exist. To fix this, you can follow the configuration instructions provided by http://maven.apache.org/pom.html#Resources. However, it’s generally better for everyone to stick to the default directory layout, as it simplifies the configuration process.

GetResourceAsStream returns null when reading properties file, From what I am seeing, your setup is just right. Having the maven resource folder and the java folder both

Читайте также:  Php fpm fastcgi params

How to load properties from resources folder?

Utilize Properties as follows:

 Properties props = new Properties(); InputStream resourceStream = Your_Class_Name.class.getResourceAsStream("config.properties")) props.load(resourceStream); 

The method uses the jars and folders specified in the classpath. To include the config.properties file which is located in the resources folder, you need to add that folder to your classpath. For instance, you can do the following:

Spring Properties File Outside jar, Note that the default file will still be loaded. But the environment-specific property file takes precedence in case of a property collision. 5.

Loading a properties file from Java package

To retrieve the Properties from a Class within the com.al.common.email.templates package, you can make use of.

Properties prop = new Properties(); InputStream in = getClass().getResourceAsStream("foo.properties"); prop.load(in); in.close(); 

Ensure all required measures are taken to handle exceptions.

If your package does not include the class, then you will have to obtain the InputStream in a slightly different manner.

InputStream in = getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties"); 

If a resource is specified with a relative path (without a leading ‘/’), it will be searched relative to the directory that represents the package of the class in getResource() / getResourceAsStream() .

By utilizing java.lang.String.class.getResource(«foo.txt») , the search operation will be conducted for the (unavailable) file /java/lang/String/foo.txt within the classpath.

If a path starts with ‘/’, the current package will be disregarded.

As an addition to Joachim Sauer’s response, in case you have to perform this action in a static context, you may execute the following method:

(The previous code snippet has been excluded as it already had exception handling.)

The two instances below pertain to the incorporation of a properties file from a sample class referred to as TestLoadProperties .

Scenario 1 involves utilizing ClassLoader to load the properties file.

InputStream inputStream = TestLoadProperties.class.getClassLoader() .getResourceAsStream("A.config"); properties.load(inputStream); 

For successful loading, the directory root/src must contain the properties file.

Scenario 2: Loading the properties file in the absence of employing ClassLoader .

InputStream inputStream = getClass().getResourceAsStream("A.config"); properties.load(inputStream); 

To ensure successful loading, the TestLoadProperties.class file requires the properties file to be located in its directory.

It should be noted that TestLoadProperties.java and TestLoadProperties.class refer to two distinct files. The former ( .java ) is typically located in the project’s src/ directory, while the latter ( .class ) is generally found in its bin/ directory.

Read a File From the Resources Folder in Scala, The first solution we can use comes from Java: the Class.getResource method which returns a URL. Let’s start by assuming we are following the

Источник

Оцените статью