0

I have a java application and I have bundled it into a JAR file using the maven shade plugin. For some reason, when I run the JAR file from target/, the JAR is not getting a file correctly from the resources folder and thus throwing a FileNotFoundException.

Error message when running 'java -jar target/speech2code-1.0-SNAPSHOT-fatjar.jar':

java.io.FileNotFoundException: file:/Users/mb/Desktop/Speech2Code-IDE/speech2code/target/speech2code-1.0-SNAPSHOT-fatjar.jar!/analyser/map.txt (No such file or directory)

The line of code where I am getting the file is:

File file = new File(getClass().getResource("/analyser/map.txt").getFile());

My Directory Structure is (map.txt is in /src/main/resources/analyser/map.txt:

/src
    /main
        /java
        /resources
               /analyser
                   /map.txt
    /test

My POM file:

<?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>org.mayur.speech2code</groupId>
<artifactId>speech2code</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.txt</include>
                <include>**/*.css</include>
                <include>**/*.fxml</include>
                <include>**/*.gram</include>
                <include>**/*.dict</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <finalName>${project.build.finalName}-fatjar</finalName>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>App</mainClass>
                    </transformer>
                </transformers>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
<repositories>
    <repository>
        <id>snapshots-repo</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases><enabled>false</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
        <id>central</id>
        <url>http://repo1.maven.org/maven2/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-core</artifactId>
        <version>5prealpha-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-data</artifactId>
        <version>5prealpha-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.fxmisc.richtext</groupId>
        <artifactId>richtextfx</artifactId>
        <version>0.7-M2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <type>maven-plugin</type>
    </dependency>
    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.10</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
</dependencies>
</project>

Any help will be appreciated!

user3650664
  • 161
  • 1
  • 14
  • i think your problem is you're using `File`.. well, a bit googling, [read this](http://stackoverflow.com/questions/4548699/load-file-within-a-jar). – Bagus Tesa Apr 18 '17 at 01:11
  • 2
    `File file = new File(getClass().getResource("/analyser/map.txt").getFile());` is the wrong way to access embedded resources, the aren't `File` references, when embedded inside a `Jar` they are Zip entries. You should be using `getClass().getResource("/analyser/map.txt")` to get a `URL` reference or `getClass().getResourceAsStream("/analyser/map.txt")` to get an `InputStream` reference to the resource – MadProgrammer Apr 18 '17 at 01:12
  • Thanks guys, I spent 2 hours figuring out and you guys tell me in 5 minutes. It works! thanks :) – user3650664 Apr 18 '17 at 01:16
  • URL.getFile() **does not** convert a URL to a valid file name. It just returns the path and query portions of a URL, which usually is not a valid file name. – VGR Apr 18 '17 at 01:25

1 Answers1

2

So I'm supposed to be using InputStream to read resources instead of converting it to a File in Java. I changed the code above and it works using InputStream. Thanks MadProgrammer and Bagus Tesa!

user3650664
  • 161
  • 1
  • 14