0

I have created a JAR file using Maven plugins and it runs. I read and write data using a .JSON file during my program. It is specified in the source code like this:

private static File getJsonFile() {
        File file = new File("src/main/resources/database/database.json");
        if (!file.exists()) {
            try {
                //noinspection ResultOfMethodCallIgnored
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

But after running my program JAR file using java -jar super-mario-bros-1.0-SNAPSHOT-jar-with-dependencies.jar it gives me this error:

java.io.IOException: The system cannot find the path specified
        at java.base/java.io.WinNTFileSystem.createFileExclusively0(Native Method)
        at java.base/java.io.WinNTFileSystem.createFileExclusively(WinNTFileSystem.java:645)
        at java.base/java.io.File.createNewFile(File.java:1045)
        at ir.sharif.math.ap2023.project.model.Database.getJsonFile(Database.java:54)
        at ir.sharif.math.ap2023.project.model.Database.getInstance(Database.java:37)
        at ir.sharif.math.ap2023.project.view.UIManager.<init>(UIManager.java:59)
        at ir.sharif.math.ap2023.project.view.UIManager.getInstance(UIManager.java:143)
        at ir.sharif.math.ap2023.project.controller.GameEngine.init(GameEngine.java:71)
        at ir.sharif.math.ap2023.project.controller.GameEngine.<init>(GameEngine.java:54)
        at ir.sharif.math.ap2023.project.controller.GameEngine.getInstance(GameEngine.java:60)
        at ir.sharif.math.ap2023.project.Main.main(Main.java:7)
java.io.FileNotFoundException: src\main\resources\database\database.json (The system cannot find the path specified)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:158)
        at com.fasterxml.jackson.core.TokenStreamFactory._fileInputStream(TokenStreamFactory.java:278)
        at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:1089)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3637)
        at ir.sharif.math.ap2023.project.model.Database.getInstance(Database.java:37)
        at ir.sharif.math.ap2023.project.view.UIManager.<init>(UIManager.java:59)
        at ir.sharif.math.ap2023.project.view.UIManager.getInstance(UIManager.java:143)
        at ir.sharif.math.ap2023.project.controller.GameEngine.init(GameEngine.java:71)
        at ir.sharif.math.ap2023.project.controller.GameEngine.<init>(GameEngine.java:54)
        at ir.sharif.math.ap2023.project.controller.GameEngine.getInstance(GameEngine.java:60)
        at ir.sharif.math.ap2023.project.Main.main(Main.java:7)

It runs without any problem after building project, but when I run this JAR file it gives me errors. This is my pom.xml by the way:

<?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>ir.sharif.math.ap2023.project</groupId>
    <artifactId>super-mario-bros</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.1</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>ir.sharif.math.ap2023.project.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

I created the JAR file using mvn clean compile assembly:single.

  • 1
    Does this answer your question? [FileNotFoundException in src/main/resources](https://stackoverflow.com/questions/23547488/filenotfoundexception-in-src-main-resources) – Rob Spoor Jun 28 '23 at 13:04
  • 1
    In short: files in `src/main/resources` should not be treated as _files_ but as _resources_. Load them as such. If the file does not exist then the call to `getResourceAsStream` will return `null`. – Rob Spoor Jun 28 '23 at 13:05

0 Answers0