File does not exist in target/classes folder of JAR, meant to be read from resources project folder

Atihska

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

Asif Bhuyan

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"));
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Read File from resources folder when code is in external jar

read from a file that is in the same folder as the .jar file

List of resources in a folder of jar file?

Read txt file from resources folder on maven Quarkus project From Docker Container

Zip folder from resources into jar

Read JSON file from project folder in ChatBot

read .html file from resources folder on server in spring boot

How to read a file from maven resources folder in GCP Cloud Function?

read html file from JSF project root folder

How to read JSON file from folder project ASP.NET

Javascript: Read text file from project folder to string

Cannot read files from resources folder

Spring boot: trying to read directory content inside resources folder produces FileNotFoundException when reading from jar

Delete files from one folder if a similarly named file does not exist in another folder

read multiple file from folder

Save file to output folder creating it if it does not exist

How best to access a file from resources folder

How to Properly Get a File from resources Folder

Copy a PNG file from the Resources to a local folder

How to read a file that is located in the project folder java

Search file name from folder and Subfolder if Exist or not

Jar can not access to resources folder

If a folder does not exist, create it

Karate API - How to read Json file from /src/test/resources folder using Junit runner?

How i can read any file from resources folder in java spring-boot AZURE function?

Loading Jar File From Different Folder

Access a file from a JAR in the same folder

Exclude Test folder and Resources from Gradle Jar build

android unzip folder from zip file and read content from that folder