error: package com.google.gson does not exist when running

58 Views Asked by At
import com.google.gson.Gson;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class CSV {
    public static void main(String[] args) {
        String csvFilePath = "csv/admin.xlsx";
        List<Person> people = readCSV(csvFilePath);
        Gson gson = new Gson();
        String json = gson.toJson(people);
        System.out.println(json);
    }

    public static List<Person> readCSV(String filePath) {
        List<Person> people = new ArrayList<>();
        try (Reader reader = new FileReader(filePath);
             CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT)) {

            for (CSVRecord csvRecord : csvParser) {
                int adminId = Integer.parseInt(csvRecord.get(0));
                int personId = Integer.parseInt(csvRecord.get(1));
                int hospitalId = Integer.parseInt(csvRecord.get(2));
                // int department = Integer.parseInt(csvRecord.get(3));
                // int joined = Integer.parseInt(csvRecord.get(4));
                // String skill = csvRecord.get(5);


                Person person = new Person();
                person.setAdminId(adminId);
                person.setPersonId(personId);
                person.setHospitalId(hospitalId);

                people.add(person);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return people;
    }
}

class Person {
    private int adminId;
    private int personId;
    private int hospitalId;

    public int getAdminId() { return adminId; }
    public void setAdminId(int adminId) { this.adminId = adminId; }

    public int getPersonId() { return personId; }
    public void setPersonId(int personId) { this.personId = personId; }

    public int getHospitalId() { return hospitalId; }
    public void setHospitalId(int hospitalId) { this.hospitalId = hospitalId; }
}
CSV.java:1: error: package com.google.gson does not exist
import com.google.gson.Gson;
                      ^
CSV.java:2: error: package org.apache.commons.csv does not exist
import org.apache.commons.csv.CSVFormat;
                             ^
CSV.java:3: error: package org.apache.commons.csv does not exist
import org.apache.commons.csv.CSVParser;

I was trying to convert a csv file to json and present the required data as output. I downloaded the google gson jar file and commons-csv jar file and added them to my reference folder in vscode. All the errors which were there initially are gone but when running it was showing the error even though all the jar files are addded.

0

There are 0 best solutions below