[英]Where to put static file in Maven project
I have created a simple Maven project with no archetype, everything is okay. 我创建了一个没有原型的简单Maven项目,一切都很好。 Then I added a
CVA.pmml
file under the main/resources
directory. 然后我在
main/resources
目录下添加了一个CVA.pmml
文件。 Afterwards, I want to read the file but get FileNotFoundException
. 之后,我想读取文件但得到
FileNotFoundException
。 I tried the following methods: 我尝试了以下方法:
Method 1: 方法1:
InputStream is = BundleTest.class.getResourceAsStream("CVA.pmml");
Method 2: 方法2:
InputStream is = BundleTest.class.getResourceAsStream("resources/CVA.pmml");
Method 3: 方法3:
InputStream is = new FileInputStream("CVA.pmml");
Method 4: 方法4:
InputStream is = new FileInputStream("resources/CVA.pmml");
None of them works. 它们都不起作用。 Any suggestion?
有什么建议吗?
Here is the screenshot of the project structure: 以下是项目结构的屏幕截图:
Method 1:
方法1:
InputStream is = BundleTest.class.getResourceAsStream("CVA.pmml");
That will look for the CVA.pmml resource in the same package as the BundleTest class. 这将在与BundleTest类相同的包中查找CVA.pmml资源。 But CVA.pmml is in the root package, whereas BundleTest is not.
但CVA.pmml位于根包中,而BundleTest则不是。
Method 2:
方法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. 这将在BundleTest类的包下的资源包中查找它。 But it's in the root package.
但它在根包中。
Method 3:
方法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. 那将在文件系统上,在您执行java命令的目录中查找它。 But it's in the classpath, embedded in the jar (or in a classpath directory)
但是它位于类路径中,嵌入在jar(或类路径目录)中
Method 4:
方法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. 那将在文件系统的目录`resources中,在你执行java命令的目录下查看它。 But it's in the classpath, embedded in the jar (or in a classpath directory)
但是它位于类路径中,嵌入在jar(或类路径目录)中
The correct way is 正确的方法是
InputStream is = BundleTest.class.getResourceAsStream("/CVA.pmml");
(notice the leading slash) (注意领先的斜线)
or 要么
InputStream is = BundleTest.class.getClassLoader().getResourceAsStream("CVA.pmml");
Create a folder named static inside resources folder and put your files there . 在resources文件夹中创建一个名为static的文件夹,并将文件放在那里。 And access it like this "/CVA.pmml"
并像这样“/CVA.pmml”访问它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.