I'm trying to import a file to use in my simple Java project, I'm currently using IntelliJ as IDEE and Gradle to build the project.
Problem:
When I try to check if the program load the resources using
URL url = getClass().getClassLoader().getResource(filePath);
function, passing "src/main/resources/File" as argument, it work fine, as I build the application (using Gradle) and run it via IntelliJ.
However, when I try to get it work with grade run via terminal, the task FAIL because it can't find the resource file, as the function return Null.
Here is my path tree:
├── build
│ ├── classes
│ │ └── java
│ │ └── main
│ │ └── it
│ │ └── uni
│ │ └── cs
│ │ └── name
│ │ └── progettofinaledatemplate
│ │ ├── controller
│ │ ├── models
│ │ └── view
│ ├── distributions
│ ├── generated
│ │ └── sources
│ │ ├── annotationProcessor
│ │ │ └── java
│ │ │ └── main
│ │ └── headers
│ │ └── java
│ │ └── main
│ ├── libs
│ ├── resources
│ │ └── main
│ │ └── it
│ │ └── uni
│ │ └── cs
│ │ └── name
│ │ └── progettofinaledatemplate->resources files
│ ├── scripts
│ └── tmp
│ ├── compileJava
│ │ └── compileTransaction
│ │ ├── backup-dir
│ │ └── stash-dir
│ └── jar
├── gradle
│ └── wrapper
└── src
└── main
├── java
│ └── it
│ └── uni
│ └── cs
│ └── name
│ └── progettofinaledatemplate
│ ├── controller
│ ├── models
│ └── view
└── resources
└── it
└── uni
└── cs
└── name
└── progettofinaledatemplate->resources files
Sorry If I repeated an already existing topic, but I can't find any solution.
What I tried:
sourceSets {
main {
resources {
srcDir 'src/main/resources'
}
}
}
Solution that worked with my Application:
After hearing the many helpful tips, I removed the nesting of the resource files, given "Tracciato3.txt" the file name and X the class I used, I moved the "Tracciato3.txt" file from:
src/main/resources/it/uni/cs/name/progettofinaledatemplate/Tracciato3.txt
to
src/main/resources/Tracciato3.txt
now if I use
URL url = X.class.getClassLoader().getResource("Tracciato3.txt");
it doesn't return null, instead It get the correct URL.
It worked for me, so I hope this will be helpful