3

I have created a simple Maven project with no archetype, everything is okay. Then I added a CVA.pmml file under the main/resources directory. Afterwards, I want to read the file but get FileNotFoundException. I 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 works. Any suggestion?

Here is the screenshot of the project structure:

Project Structure

Yi Luo
  • 153
  • 2
  • 15

2 Answers2

5

Method 1:

InputStream is = BundleTest.class.getResourceAsStream("CVA.pmml");

That 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");

That will look for it in the resources package under the package of the BundleTest class. But it's in the root package.

Method 3:

InputStream is = new FileInputStream("CVA.pmml");

That wil look it on the file system, in the directory from which you executed the java command. But it's in the classpath, embedded in the jar (or in a classpath directory)

Method 4:

InputStream is = new FileInputStream("resources/CVA.pmml");

That wil look it on the file system, in the directory `resources, under the directory from which you executed the java command. But it's in the classpath, embedded in the jar (or in a classpath directory)

The correct way is

InputStream is = BundleTest.class.getResourceAsStream("/CVA.pmml");

(notice the leading slash)

or

InputStream is = BundleTest.class.getClassLoader().getResourceAsStream("CVA.pmml");
Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
-2

Create a folder named static inside resources folder and put your files there . And access it like this "/CVA.pmml"