The specified exec-maven-plugin file cannot be found [duplicate]

This question has already been answered here: How to use resource files in a Java application (1 answer) Access to jar file resources (1 answer) Closed 7 months ago.

Maven does not find a file that is located in the resourses folder. Tell me how to solve this problem, how and what to change?

Mistake:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default-cli) on project pathToCSVAndDateToInt: An exception occured while executing
the Java class. file.csv (Не удается найти указанный файл) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Code java:

package org.example;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class main {
    public static void main (String[] args) throws IOException, CsvValidationException {
        CSVReader csvReader = new CSVReader(new FileReader("file.csv"));
        String[] arrayString;
        while((arrayString = csvReader.readNext()) != null) {
            System.out.println(arrayString);
        }
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>pathToCSVAndDateToInt</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>org.example.main</mainClass>
                    <arguments>
                        <argument>-Dproperty=first</argument>
                        <argument>-Dproperty=second</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The file itself is located in the resources folder:

enter a description of the image here

Author: arthur, 2020-07-27

1 answers

Try this

 ClassLoader classloader = Thread.currentThread().getContextClassLoader();
 InputStream is = classloader.getResourceAsStream("file.csv");

If it is difficult to understand, then you can get the file.

URI uri = classloader.getResource("file.csv").getURI();

CSVReader csvReader = new CSVReader(new FileReader(new File(uri)));
 0
Author: Aziz Umarov, 2020-07-28 08:36:57