尽管处于依赖关系中,但包不存在

Package doesn't exist despite being in dependencies

我在使用 Maven 时遇到问题,xml 文件不是我创建的,它和我的代码对我的导师来说工作正常,即使我将我在 IntelliJ 中使用的整个项目发送给她也是如此。 我根据需要使用 IntelliJ idea Community 2020.2,Java 8,11,14,我刚刚安装了 Maven 3,希望它能解决我的问题,但它没有,至少 mvn -v 从现在命令行。

如果我尝试导入 com.opencsv 它会抛出错误:

java: package com.opencsv does not exist

IntelliJ 识别包,甚至为我等自动完成方法,但在 IntelliJ 终端和 windows 命令行中尝试编译时总是失败。

我可以通过 maven 终端和 cmd 运行 其他程序。 IntelliJ 在依赖项中识别 com.opencsv。

第一次使用maven,请耐心等待:*

正如我所说,pom 和我的代码适用于我的导师,但无论如何,这是 pom:

<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>TownDbWS1920</groupId>
    <artifactId>TownDbProgramingExercise</artifactId>
    <version>1.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>3.8</version>
        </dependency>
    </dependencies>

</project>

正如我所说,代码很好并且适用于其他人,但无论如何:


import com.opencsv.CSVReader;

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

/**
 * Zettel 0 - Aufgabe 2
 */
public class ReadCSV_Task2 {

    private static String FILENAME = "data.csv";

    public static void main(String[] args) {
        System.out.println("---Aufgabe 1.1---");
        fetchAll_CSV_NUM(FILENAME);
        System.out.println("---Aufgabe 1.2---");
        fetchAll_CSV_ASSOC(FILENAME);

        System.out.println("Compiled, no syntactical failures =)");
    }

    /**
     * Aufgabe 1.1
     * @param fileName die CSV-Datei
     * @return die Daten der CSV-Datei in einer Arrayliste
     */
    public static List<String[]> fetchAll_CSV_NUM(String fileName) {
        FileReader fileReader = null;
        List<String[]> words = null;
        try {
            fileReader = new FileReader(fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        CSVReader csvReader=new CSVReader(fileReader);
        try {
            csvReader.readNext(); //read the first line to dispose of it
            words =new ArrayList<>(csvReader.readAll());//Read all the other lines into an arraylist
        } catch (IOException e) {
            e.printStackTrace();
        }
        return words;
    }

    /**
     * Aufgabe 1.2
     * @param fileName die CSV-Datei
     * @return die Daten der CSV-Datei in einer Mapliste
     */
    public static List<Map<String, String>> fetchAll_CSV_ASSOC(String fileName) {
        FileReader fileReader=null;
        List<Map<String, String>> maps;
        String[] headLine=null;
        List<String[]> words=null;
        try {
            fileReader = new FileReader(fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        CSVReader csvReader=new CSVReader(fileReader);
        try {
            headLine=csvReader.readNext();//Read the first line to get the keys
            words =new ArrayList<>(csvReader.readAll());//Read the rest into a list
        } catch (IOException e) {
            e.printStackTrace();
        }
        maps=new ArrayList<>();
        for(String[] line:words){//For each line
            Map<String, String> cmap= new HashMap<>();
            for(int i=0;i<line.length;i++){//Assign to a key
                cmap.put(headLine[i],line[i]);
            }
            maps.add(cmap);
        }
        return maps;
    }
}

在您的代码中,您没有导入 CSVReader,也没有使用它的 fully-qualified 名称。

将此行与您的其他导入一起添加:

import com.opencsv.CSVReader;

编辑:我看到您显然已经尝试过了,但这就是输出您报告的错误消息的原因。

既然你说 mvn compile 很开心但 IntelliJ 不开心,最可能的原因是你没有告诉 IntelliJ 打开你的代码 作为 Maven 项目 ,所以它没有打开它的 Maven 集成。从您的 IntelliJ 项目中删除此程序(不要删除内容,只需取消它与 IDE 的链接)和 open it as a Maven project.

复制我在这里找到的答案:

导出 IDE 设置(键盘映射、插件)。您可以通过转到文件 | 导出它。管理 IDE 设置 |导出设置...它将导出一个 zip 文件,您可以稍后在恢复默认 IDE 配置时导入它

转到文件 |管理 IDE 设置 |恢复默认设置... IDE 将重新启动并恢复为默认配置。此命令 re-generated path.macros.xml 文件缺少正确的配置。请注意,您可以在 /options 目录中归档

导入导出的 zip 文件文件 |管理 IDE 设置 |导入设置...