The java application does not read my files (maven project)

I have an application in a simple Java project. However, I need to insert this project into the Maven project. So, I basically did a simple Maven project, and I copied and pasted all my classes into it. I need war to run on the server, and I need to run Main as a Java application, because this application configures the military application. However, when I run Main, I get some errors that I did not have before:

java.io.FileNotFoundException: resources \ config.properties (the system cannot find the path specified)

when the code has:

input = new FileInputStream("resources/config.properties"); 

That didn't work either:

 faceDetector = new CascadeClassifierDetector("D:/retinoblastoma/workspace/Resources/CascadeClassifiers/FaceDetection/haarcascade_frontalface_alt.xml"); 

How can i fix this?

+5
source share
2 answers

If you are using a "simple maven project", the maven path is the src/main/resources folder. Have you configured your pom.xml to do something else?

If you created the jar creation part correctly, the correct way is to get a file that is in the classpath :

 getClass().getResourceAsStream("/path/to/resource.ext"); 

The leading slash is important!

If the file is NOT in your classpath (in other words, it will be so if it doesn't work), you probably need to configure maven to use a different resource directory.

You do like this (change the arguments if necessary):

 <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 in your classpath and the above code will work. See the related documentation for more details.

+3
source

In a simple Mavne project, all resources should be located in src / main / resources. You can get the properties file then (for a non-static method):

Properties prop = new Properties(); prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));

For the static method, use: <Class name>.class.getClassLoader().getResourceAsStream("config.properties");

For more information, see this link: Read Property File

+3
source

All Articles