0

I'm using the maven-assembly-plugin to generate a single jar file with all dependencies using the command line:

mvn clean compile assembly:single

It compile all .java files from the directory src/main/java/ to a .jar file in target/.

I have a class inside of my src/main/java/ directory that read a .json file which is located next to him using this code:

File file = new File("src/main/java/path/to/test.json");
content = FileUtils.readFileToString(file, "UTF-8");

So I will need to have access to this .json file in my generated .jar.

I include this .json in maven using this config in <build>:

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>src/main/java/path/to/test.json</include>
        </includes>
    </resource>
</resources>

Everything compile successfully and when I tried to run my .jar file in target/ directory using java -jar ./target/my-jar-file.jar it's work great.

Problem is that now I want to be able to move my .jar file wherever I want. But when I execute it in another place I get the error message:

java.io.FileNotFoundException: File 'src/main/java/path/to/test.json' does not exist

So obviously the .json file has not been included in my generated .jar.

Has somebody an idea what I have missed ?

johannchopin
  • 13,720
  • 10
  • 55
  • 101
  • 1
    you cannot use File to access a jar file entry. It isn’t a file on disk. – Thorbjørn Ravn Andersen Jan 04 '20 at 16:03
  • 1
    You should put the file in `src/main/resources`, not in `src/main/java` and you can't use `File` for resources inside a jar file. You should use `Class.getReourceAsStream` (or `ClassLoader.getResourceAsStream`) to load it. – Mark Rotteveel Jan 04 '20 at 16:26

2 Answers2

3

try to extract test.json file from jar to resources folder in current project using maven plugin , please try

 Java Code changes : -
 File file = new File("src/main/resources/test.json");
 content = FileUtils.readFileToString(file, "UTF-8");

pom.xml changes :- 
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>unpack-jar-file</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>groupID</groupId>
                                    <artifactId>artifactId</artifactId>
                                    <version>version</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                               <outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
                                    <includes>**/*.json</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build

Note : In case jar is present in local instead of any central repository , please use below tag

${project.build.directory}/localrepo

Sarjit
  • 799
  • 7
  • 9
2

You will need to load the file from the Classpath (assuming the dependency jar is on the classpath), not from the file system. See this answer for how to do that:

How to really read text file from classpath in Java

KevinB
  • 1,102
  • 2
  • 8
  • 19
  • Well ok but if I want to move my jar in my `Desktop` for example I will get the same error right ? – johannchopin Jan 04 '20 at 15:59
  • @johannchopin, please check whether the jar file contains the .json file by unzipping the jar file. – PythonLearner Jan 04 '20 at 16:01
  • 1
    If you know the location of a zip/jar, then you can read the contents of that zip/jar using java.util.zip. However, if this jar is a part of your application, it should be included on the classpath when the application is executed. Your original code above is not loading the JSON from the JAR you built, it is loading it from a relative file path, which is why when you moved the jar the relative file path changed and your code broke. – KevinB Jan 04 '20 at 16:06
  • This JSON file exists (it has been included) in the JAR that you are running?If so, then it is already on the classpath and you simply need to load it using the mechanisms I linked. – KevinB Jan 04 '20 at 16:09