Where to put static file in Maven project
I created a simple Maven project without archetype, everything is fine. Then I added the file CVA.pmml
to the directory main/resources
. Subsequently, I want to read the file, but we get FileNotFoundException
. I've tried the following methods:
Method 1:
InputStream is = BundleTest.class.getResourceAsStream("CVA.pmml");
Method 2:
InputStream is = BundleTest.class.getResourceAsStream("resources/CVA.pmml");
Method 3:
InputStream is = new FileInputStream("CVA.pmml");
Method 4:
InputStream is = new FileInputStream("resources/CVA.pmml");
None of them work. Any suggestion?
Here is a screenshot of the project structure:
source to share
Method 1:
InputStream is = BundleTest.class.getResourceAsStream("CVA.pmml");
This will look for the CVA.pmml resource in the same package as the BundleTest class. But CVA.pmml is in the root package whereas BundleTest is not.
Method 2:
InputStream is = BundleTest.class.getResourceAsStream("resources/CVA.pmml");
This will look for it in the resource bundle in the BundleTest class. But this is in the root package.
Method 3:
InputStream is = new FileInputStream("CVA.pmml");
This will appear on the filesystem in the directory from which you ran the java command. But this is in the classpath embedded in the jar (or in the classpath directory)
Method 4:
InputStream is = new FileInputStream("resources/CVA.pmml");
This will appear on the filesystem under the `resources directory, in the directory from which you ran the java command. But this is in the classpath embedded in the jar (or in the classpath directory)
The right way
InputStream is = BundleTest.class.getResourceAsStream("/CVA.pmml");
(note the leading slash)
or
InputStream is = BundleTest.class.getClassLoader().getResourceAsStream("CVA.pmml");
source to share