I'm programming in java and I'm using maven. The pom is this:
<?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>it.polimi.ingsw</groupId>
<artifactId>PS60</artifactId>
<version>1.0-SNAPSHOT</version>
<name>PS60</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations-java5</artifactId>
<version>19.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>it.polimi.ingsw.ps60.Launcher</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>it.polimi.ingsw.ps60.Launcher</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
As you can see I set in the that the resource files are located in src/resources. I have already checked that there are all the resources that I want in the jar (not in folders called src and resources but directly in the root of the jar or in the folder where they were in the src/resources). When I was coding this program I was accessing all my resources like this:
imageToMerge.add(ImageIO.read(new File("src/resources/board/Buildings/1 floor.png")));
or like this:
outputStream = new FileOutputStream("src/resources/save");
or other alternatives. Problems cames when I do the package of the jar: I can't access my resources anymore. I read a lot of tutorials but none of them suited with my program for some reason: I tried for example:
BufferedImage buff = ImageIO.read(getClass().getResourceAsStream("pathToImage"));
I tried both the resource path relative to the jar output "board/Buildings/1 floor.png" for example or the full path: "src/resources/board/Buildings/1 floor.png" and also (only in order to try) with just the name of the file "1 floor.png" but none of them worked. With the same attempt previously listed I also tried:
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
outputStream = new FileOutputStream(new File(Objects.requireNonNull(classLoader.getResource("pathToImage")).getFile()));
but this impressively only works when I was running the program in IntelliJ and not in a jar and the path without src/resources/ (so only "board/Buildings/1 floor.png") ware the only one to work worked. Now I'm wondering if I have not properly declared my resources or something else but I have still not found a solution.