0

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.

  • Possibly (I am not sure, since I don't work with android) related? [Loading image resource](https://stackoverflow.com/q/9864267). – Pshemo Jun 05 '20 at 17:17
  • One thing's for sure: pictures shouldn't be under the `src` folder. – Federico klez Culloca Jun 05 '20 at 17:25
  • You're confusing files with resources. At runtime, the `src` folder will not even necessarily be there: it's the *source* folder. Maven will deploy the content of any resource folder to the classpath: so the corresponding path will be `ImageIO.read(getClass().getResourceAsStream("/board/Buildings/1 floor.png")`. However, `"1 floor.png"` is not a valid resource name (because of the whitespace), so you need to rename it. [This question](https://stackoverflow.com/questions/61531317/) is JavaFX-specific, but explains how a lot of this works. – James_D Jun 05 '20 at 17:31
  • For your output stream, I'm not even clear what you're attempting to do. You can't update the contents of the jar file. – James_D Jun 05 '20 at 17:34
  • Thank you a lot @James_D . Actually I only forgot the first "/" but you let me notice it. So there are no methods to update a file in the jar with the outputStream? Anyhow post your first comment as an answer because that "/" really fixed my problem. – Vincenzo Greco Jun 05 '20 at 17:41

0 Answers0