在Maven中创建由Maven-archetype-quickstart创建的Java项目中的属性文件 [英] Reading Properties file in Maven Java Project created by maven-archetype-quickstart

查看:808
本文介绍了在Maven中创建由Maven-archetype-quickstart创建的Java项目中的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (properties = new Properties())。load(DbCopy.class.getClassLoader()
.getResourceAsStream(config.properties));

它会抛出异常:

 线程中的异常mainjava.lang.NullPointerException 
在java.util.Properties.load(Properties.java:284)
at benz.bnp.db.DbCopy .main(DbCopy.java:77)

感谢您的帮助!

解决方案

如果目标文件夹包含属性文件,请查看目标文件夹。希望它不会包含。要包括您的属性文件,请修改您的pom:

 < build> 
< resources>
< resource>
< directory> $ {project.basedir} / src / main / java< / directory>
< filtering> true< / filtering>
< includes>
< include> ** / *。properties< / include>
< / includes>
< / resource>
< / resources>
< / build>

将其添加到您的POM.xml并为您的属性文件配置相同。


I'm trying to read a properties file using the below code on a Java Project created using Maven ArchetypeId=maven-archetype-quickstart

(properties = new Properties()).load(DbCopy.class.getClassLoader()
   .getResourceAsStream("config.properties"));

And it throws exception:

Exception in thread "main" java.lang.NullPointerException
  at java.util.Properties.load(Properties.java:284)
  at benz.bnp.db.DbCopy.main(DbCopy.java:77)

Thanks for helping !

解决方案

Have a look at the target folder, if it contains your properties file. Hopefully it will not contain. To include your property file edit your pom:

<build>        
<resources>
                        <resource>
                            <directory>${project.basedir}/src/main/java</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/*.properties</include>
                            </includes>
                        </resource>
     </resources>
</build>

Add it to your POM.xml and configure the same for your properties file.

这篇关于在Maven中创建由Maven-archetype-quickstart创建的Java项目中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆