我有一个 Java 简单项目中的应用程序。但是,我需要将此项目粘贴到 Maven 项目中。因此,我基本上制作了一个简单的 Maven 项目,并将所有类复制并粘贴到其中。我需要在服务器中运行一个war,并且我需要像Java应用程序一样运行Main,因为这个应用程序配置了war应用程序。但是,当我运行 Main 时,我遇到了一些以前没有的错误:
java.io.FileNotFoundException: resources\config.properties (系统找不到指定的路径)
当代码中是:
input = new FileInputStream("resources/config.properties");
这也不起作用:
faceDetector = new CascadeClassifierDetector("D:/retinoblastoma/workspace/Resources/CascadeClassifiers/FaceDetection/haarcascade_frontalface_alt.xml");
我怎样才能解决这个问题?
在一个简单的 Maven 项目中,所有资源都应位于 src/main/resources 中。然后您可以通过以下方式获取属性文件(对于非静态方法):
Properties prop = new Properties();
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
对于静态方法,请使用:<Class name>.class.getClassLoader().getResourceAsStream("config.properties");
请参阅此链接了解更多信息:读取属性文件 http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)