我的Java应用程序无法读取我的文件(Maven项目) [英] My java application does not read my files (maven project)
问题描述
我在一个Java简单项目中有一个应用程序.但是,我需要将此项目粘贴到Maven项目中.因此,我基本上做了一个简单的Maven项目,然后将所有类复制并粘贴到该项目中.我需要在服务器上运行war,并且需要像Java应用程序一样运行Main,因为此应用程序配置了war应用程序.但是,当我运行Main时,遇到了一些以前没有的错误:
I have an application in a Java simple project. However, I need to paste this project to a Maven project. So, I basically made a simple Maven project, and I copied and pasted all my classes into it. I need a war to run in a server, and I need to run a Main like a Java application, because this application configures the war application. However, when I run Main, I get some errors that I wasn't having before:
java.io.FileNotFoundException:resources \ config.properties(系统找不到指定的路径)
java.io.FileNotFoundException: resources\config.properties (The system cannot find the path specified)
代码中的时间是:
input = new FileInputStream("resources/config.properties");
这也不起作用:
faceDetector = new CascadeClassifierDetector("D:/retinoblastoma/workspace/Resources/CascadeClassifiers/FaceDetection/haarcascade_frontalface_alt.xml");
我该如何解决?
推荐答案
如果您使用的是简单的maven项目",则maven的方法是使用 src/main/resources
文件夹.您是否将pom.xml配置为执行其他操作?
If you're using a "simple maven project", the maven way is for there to be a src/main/resources
folder. Did you configure your pom.xml to do something different than that?
如果您正确完成了jar创建部分,则获取位于类路径上的文件的正确方法是:
If you've done the jar creation portion correctly, the right way to get a file that's on the classpath is:
getClass().getResourceAsStream("/path/to/resource.ext");
正斜杠很重要!
如果文件不在您的类路径中(换句话说,如果上面的方法不起作用,则可能是这种情况),您可能需要将maven配置为使用其他资源目录.
If the file is NOT on your classpath (in other words, this will be the case if the above doesn't work), you probably need to configure maven to use a different resources directory.
您可以按这样做(适当地更改参数):
You do that like this (change the arguments as appropriate):
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
...
</build>
然后,您的文件将位于您的类路径中,并且上面的代码将起作用.阅读上面链接的文档以获取更多信息.
Then, your file will be on your classpath and the above code will work. Read the linked documentation above for more information.
这篇关于我的Java应用程序无法读取我的文件(Maven项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!