简体   繁体   English

在Maven项目中放置静态文件的位置

[英]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.

相关问题 将项目迁移到Maven,在其中放置config文件夹 - Migrated project to Maven, where to put config folder 我应该在web应用程序(maven项目)中将java.util.logging的logging.properties文件放在哪里? - Where should I put logging.properties file for java.util.logging in web application (maven project)? 在没有WEB-INF目录的Maven / Java项目中放置Spring上下文文件的位置? - Where to put Spring context file for a Maven/Java project with no WEB-INF directory? 在多模块 maven 项目中将跨不同模块使用的属性文件放在哪里? - Where to put property file that is used across different modules in a multi-module maven project? 放置属性文件的位置(与Maven程序集有关) - Where to put properties file (in connection with maven assembly) 我应该将主要实现放在多模块Maven项目中的哪个位置? - Where should I put the main implementation in a multi module Maven project? 我应该将IDL文件和生成的代码放在Maven项目的哪里? - Where should I put IDL files and generated code in Maven project? 在 maven 项目中将 log4j.properties 放在哪里? - Where to put log4j.properties in a maven project? 将xsl文件放在Java项目中的哪里? - Where to put xsl file in java project? 将jar文件放在ruby on rails项目中的哪里? - Where to put jar file in ruby on rails project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM