javamavenvisual-studio-codeopencsv

Solving java:1: error: package com.opencsv does not exist on VSCode


I am getting the following error

SortingTester.java:1: error: package com.opencsv does not exist
import com.opencsv.CSVWriter;
                  ^
SortingTester.java:109: error: cannot find symbol
            CSVWriter writer = new CSVWriter(outputfile);
            ^
  symbol:   class CSVWriter
  location: class SortingTester
SortingTester.java:109: error: cannot find symbol
            CSVWriter writer = new CSVWriter(outputfile);
                                   ^
  symbol:   class CSVWriter
  location: class SortingTester

For this code block:

import com.opencsv.CSVWriter;

public static void writeCSV (String[] arr, File filepath) {
try {
FileWriter outputfile = new FileWriter(filepath, true);
CSVWriter writer = new CSVWriter(outputfile);
writer.writeNext(arr);
writer.close();
System.out.println(Arrays.toString(arr));
}

        catch (IOException e) {
            e.printStackTrace();
        }
    }

For reference, I am using VSCode and its integrated Maven dependencies. I have a pom.xml attached to my project folder, and I added the most recent version to my pom.xml

<dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.9</version>
        </dependency>

Even though the dependency is integrated properly--when I open Java Projects on the side pull-down tab, opencsv-5.9.jar shows in the Maven Dependencies section--the program doesn't execute properly. I have no idea what's going on, and my problem is different from all other stack overflow questions I've seen so far.

Help needed, thanks


Solution

  • After some debugging, I've found a partial solution. For all you VSCoders, I'm not sure if this is just me with my unique machine, but the reason the "package doesn't exist" is since I am only trying to Run Code (this correlates to the Output Console below). The Output Console, for some reason, doesn't support OpenCSV, or isn't connected to Maven Dependencies/Java Libraries, etc. Instead, I "Ran Java," which compiles and executes your code on the built-in terminal in VSCode. There, OpenCSV worked fine (so long as you guys have it added in your pom.xml and properly import the library).

    And then, just a small tip I've found from searching before this, if your code doesn't compile still after ALL this, check a couple things:

    1. That your pom.xml has the correct version of the dependency added
    2. That Maven actually integrates/downloads the library. You can check this through either Java Projects OR Maven Dependencies on the pull-down tab on the left (for VSCode-users only)
    3. If it still doesn't work, try reloading the project :D
    4. This means either restarting your IDE/text editor, right clicking on your project folder and clicking "Reload Project," or clicking "Rebuild Project"