0

I am trying to read. a config from my resources folder in Java project from my deployed code. I am able to read from my local laptop but after deployment as JAR .manifest file, it says path does not exist.

So my Java maven project str: src/main/java/.. and config path as follows:

Java code to read this config where file.exists() always returns false.

Trial 1: When config path is : src/main/resources/config.yaml.

File configPath = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("config.yaml")).getFile());
if (!configPath.exists()) {
        Log("ERROR", "Config file does not exist "); // this is printed
      }

Trial 2: When config path is src/main/resources/feed/configs/config.yaml.

File dir = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("feed/configs")).getFile());
if (!dir.exists()) {
        Log("ERROR", "Config folder does not exist, "ERROR"); // THIS IS PRINTED 
        return;
      }
File[] configFiles = configPath.listFiles(); // NOT EXECUTED AS ABOVE IS RETURNED

Atihska
  • 4,803
  • 10
  • 56
  • 98
  • 2
    Assuming you're using maven, then you should load the resource using `Class#getResource` as the "resource" will NOT be accessible as a `File` (it's contained within the Jar file itself). So you should use `getClass().getResource("config.ymal")` or `getClass().getResourceAsStream("config.ymal")` depending on your needs. Also, you won't be able to determine if a "directory" exists within the Jar (or more importantly, the class loader context) at runtime (reliably) – MadProgrammer Apr 03 '22 at 23:33
  • @MadProgrammer So no `getClassLoader()` ? – Atihska Apr 03 '22 at 23:43
  • There are conditions where you might need `getClassLoader`, but generally, I've not run into them (personally) - the point is, don't use `File` to reference embedded resources – MadProgrammer Apr 03 '22 at 23:43
  • @MadProgrammer I get null input stream this case. – Atihska Apr 04 '22 at 01:43
  • 2
    Sorry, it should probably be more like `getClass().getResource("/config.ymal")` (ie the path should be `/config.ymal`). If you still have issues, make sure the file is been included in the jar file, you can simply unzip it to check and make use of `getClassLoader` if it helps fix the issue – MadProgrammer Apr 04 '22 at 01:54

1 Answers1

0

Since you have added the maven tag, I am assuming you are using maven.

As the .yaml is inside the resources folder you should be using getResourceAsStream()

/src/main/resources/config.yaml:

first: value1
second: value2

To read the file and its content:

import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

public class Example {

    InputStream inputStream = null;
    final Properties properties = new Properties();

    public Example() {
        try {
            inputStream = 
                this.getClass().getClassLoader().getResourceAsStream("config.yaml");
            properties.load(inputStream);
        } catch (IOException exception) {
            LOG("ERROR", "Config file does not exist ");
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (Exception e) {
                    LOG("ERROR", "Failed to close input stream");
                }
            }
        }
     }

    public printValues(){
        LOG("INFO", "First value is: " + properties.getProperty("first"));
    }
}
Asif Bhuyan
  • 128
  • 8