FileNotFoundException starting jar - can't see file in resources folder

Nikolas picture Nikolas · Sep 8, 2014 · Viewed 8.9k times · Source

I want that my application will be OS independent. Therefore my config.properties and log file are stored in resources folder and I get these resources with relative path. Here is my project structure.project structure

Here is my AppConfig class:

public final class AppConfig {

private static final String RELATIVE_PATH_TO_PROPERTIES = "./src/main/resources/config.properties";
public static final String RELATIVE_LOG_PATH = "./src/main/resources/err_action.log";

private static Properties props = initProperties();
public static final String HOST = props.getProperty("ip_address");

public static final int PORT = Integer.valueOf(props.getProperty("port"));
public static final int MAX_USERS = Integer.valueOf(props.getProperty("max_number_of_users"));
public static final int NUM_HISTORY_MESSAGES = Integer.valueOf(props.getProperty("last_N_messages"));

private static Properties initProperties() {
    Properties properties = null;
    try (FileInputStream input = new FileInputStream(RELATIVE_PATH_TO_PROPERTIES)) {
        properties = new Properties();
        properties.load(input);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return properties;
}
}

As you can see, I specify relative path for properties and log files. I create jar with maven and when I run it, I receive

java.io.FileNotFoundException: ./src/main/resources/err_action.log (No such file or directory)

UPD Here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0          http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>chat</groupId>
<artifactId>Client-Chat</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
    <java_version>1.8</java_version>
</properties>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java_version}</source>
                <target>${java_version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>nikochat.com.app.Main</mainClass>
                        <!--<mainClass>nikochat.com.app.RunClient</mainClass>-->
                    </manifest>
                </archive>
            </configuration>
        </plugin>

    </plugins>
</build>
</project>

I use Intellij Idea and run maven package command, result of which is next output:

[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building Unnamed - chat:Server-Chat:jar:1.0 [INFO]
task-segment: [package] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 2 resources [INFO] [compiler:compile {execution: default-compile}] [INFO] Nothing to compile - all classes are up to date [INFO] [resources:testResources {execution: default-testResources}] [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /home/nikolay/IdeaProjects/Chat/src/test/resources [INFO] [compiler:testCompile {execution: default-testCompile}] [INFO] Nothing to compile - all classes are up to date [INFO] [surefire:test {execution: default-test}] [INFO] Surefire report directory: /home/nikolay/IdeaProjects/Chat/target/surefire-reports

------------------------------------------------------- T E S T S ------------------------------------------------------- Running AppConfigTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.129 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 2

[INFO] [jar:jar {execution: default-jar}] [INFO] Building jar: /home/nikolay/IdeaProjects/Chat/target/Server-Chat-1.0.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11 seconds [INFO] Finished at: Mon Sep 08 09:47:18

EEST 2014 [INFO] Final Memory: 18M/154M [INFO]

at the beginning I create Server-Chat jar for running server part of application, than I change artifactId to Client-Chat and manifest mainClass to create client part of application. Both parts I run in terminal typing command: java -jar Server-Chat-1.0.jar or java -jar Client-Chat-1.0.jar respectively.

Here is the output of server:

java.io.FileNotFoundException: config.properties (No such file or directory) at java.io.FileInputStream.open(Native Method)

And client:

eaProjects/Chat/target $ java -jar Client-Chat-1.0.jar java.io.FileNotFoundException: config.properties (No such file or directory)

Answer

Adi picture Adi · Sep 8, 2014

src/main/resources is maven convention to have resources file. When maven build jar/war artifact, it adds all files/directories from src/main/resources to classpath of resulting artifact.

There is no src/main/resources available at runtime.

In your case, you can update your program to read these files without giving any path. like below

private static final String RELATIVE_PATH_TO_PROPERTIES = "config.properties";
public static final String RELATIVE_LOG_PATH = "err_action.log";