0

I have a Maven project that I use Eclipse to write. I have a hierarchical src/main/resources such that file1.zip is located in a main directory, which file2.zip and file3.zip are located in a sub-directory of file1.zip.

src/main/resources
    models/modelA/file1.zip
    models/modelA/subdirectory1
          file2.zip
    models/modelA/subdirectory2
          file3.zip

my pom.xml file has this included

        <resources>
        <resource>
            <directory>src/main/resources/models</directory>
            <includes>
                <include>**/*.zip</include>
            </includes>
        </resource>
    </resources>

I load the resource as

String file1 = "/models/modelA/file1.zip";
String file2 = "/models/modelA/subdirectory1/file2.zip";
String file2 = "/models/modelA/subdirectory1/file3.zip";
InputStream is1 = getClass().getResourceAsStream(file1);
InputStream is2 = getClass().getResourceAsStream(file2);
InputStream is3 = getClass().getResourceAsStream(file3);

When running the program after mvn package, everything is works. But when I try to run this in Eclipse, is2 and is3 are always null.

The src/main/resources are in my class path. How can I get this to run also in Eclipse?

I should mention I have looked at the SO posts enter link description here enter link description here and many others that do not give an answer to the difference between it working with Eclipse and Maven

mkunkel
  • 243
  • 3
  • 16
  • 1
    Maybe this might help: https://stackoverflow.com/a/22117293/4121573 – Adonis Jan 07 '19 at 11:11
  • The only issue I see is that resource directory is `src/main/resources/models` instead of `src/main/resources/` which would make `/models` part unnecessary when loading the resource. But then `is1` would also be null, hm.. Maybe it's not null because of previous experiments? BTW, you can check if Maven worked as expected by looking into `target/classes` dir - there you'd see your zip files and folders. – Stanislav Bashkyrtsev Jan 07 '19 at 11:59
  • @Adonis I did what that post said, and still get is2 and is3 as null – mkunkel Jan 07 '19 at 12:13
  • @StanislavBashkyrtsev target/classes has all files and the package runs accordingly – mkunkel Jan 07 '19 at 12:13
  • Well if what you see in `target/classes` is correct - then the problem is not in Maven. That's where Maven did its part. The other part is loading resource - maybe paths are incorrect or something. – Stanislav Bashkyrtsev Jan 07 '19 at 13:18
  • Possible duplicate of [Why does Eclipse not allow me to create sub folders correctly?](https://stackoverflow.com/questions/22116913/why-does-eclipse-not-allow-me-to-create-sub-folders-correctly) – Rich Jan 07 '19 at 14:35
  • @Rich your contribution was exactly first comment, in which I replied that I had already performed that operation and still not working. – mkunkel Jan 08 '19 at 16:26
  • Is it a bad practice to use multiple subfolders in the resources folder? For instance mimic the main structure of folders? I often also have path issues and I am wondering whether my problem is to create multiple subfolders – DTK Aug 02 '21 at 12:54
  • I was just looking for this issue too and found this post that might help https://stackoverflow.com/questions/8504204/maven-copy-multiple-files-from-different-resource-folders-to-a-single-tagert-fol – DTK Aug 02 '21 at 13:04

0 Answers0