1

I found that I cannot just run import com.opencsv.CSVReader; statement in my JDK framework.

Edited1: I was asked to make it clearer:

import com.opencsv.CSVReader;

throws

The import com.opencsv cannot be resolved

(using VSCode)

How can I add OpenCSV to my framework?

This question arose from import csv to JTable

Edited2: It is now clear that AdoptOpenJDK is not the issue and that the question is independent from the jdk framework and version. Thus I changed the question (and respective tags) from

OpenCSV - CSVReader alternative in AdoptOpenJDK

to

How to implement OpenCSV - CSVReader in a JDK

questionto42
  • 7,175
  • 4
  • 57
  • 90
  • 1
    Is the jar visible on the classpath? – Thorbjørn Ravn Andersen Jun 22 '20 at 09:08
  • 1
    Please expand on "does not seem to support". What is the problem that you see? – RealSkeptic Jun 22 '20 at 09:11
  • Perhaps not clear enough. I simply cannot run the import statement `import com.opencsv.CSVReader;` – questionto42 Jun 22 '20 at 10:59
  • 1
    Make sure you have added the OpenCSV JAR file to VS Code first, regardless of what version of Java you are using (Oracle's or others). See [here](https://stackoverflow.com/questions/50232557/visual-studio-code-java-extension-howto-add-jar-to-classpath). If you don't have the JAR for OpenCSV, you can download the latest version from Maven [here](https://repo1.maven.org/maven2/com/opencsv/opencsv/5.2/opencsv-5.2.jar). – andrewJames Jun 22 '20 at 12:17
  • @andrewjames, could you please add an answer with the contents of your comment so that I can accept it? Of course it all worked with the added jar now. – questionto42 Jun 24 '20 at 12:29
  • Thank you to all, my question was really bad because I simply did not know about this probably very basic adding of jar files, I did not even understand the first comment of @Thorbjørn Ravn Andersen :). – questionto42 Jun 24 '20 at 12:31
  • 1
    @Lorenz We have all been there. The classpath is one of the hard things to learn with Java, so you just have cleared the first hurdle towards mastery. Now go study :) – Thorbjørn Ravn Andersen Jun 24 '20 at 13:23
  • 1
    I am glad you solved your problem - that's great. If I wrote an answer, it would effectively be a duplicate of the answer(s) in the other question I linked to. This site works hard to discourage duplicate questions and answers. And don't worry about the supposed badness of your question - sometimes (often?) we don't know what we don't know. You improved the question - which is exactly the right thing to have done. – andrewJames Jun 24 '20 at 13:35
  • 1
    If you think it would be useful to other people who may hit the same problem you faced (and not duplicate info), you are welcome to write your own answer to your own question. Or you can delete your question. You can also add new info to the other question, if that is appropriate. Personally, I think any of the above steps would be OK (but I am also still learning about how this site works). – andrewJames Jun 24 '20 at 13:36
  • @andrewjames Would you be willing to answer *AND* flag it as duplicate? because this would be the best way to include it in the SO pattern, if you ask me. And I do not want to accept your humble remark that you are still learning :)), your points considered. It will just be a way for the user to find the right question, tags and links in case she happens to have the unlikely but not impossible issue that exactly this import is the very first needed jar file. If it is not you, I will add your comment as answer and flag the whole question as duplicate. – questionto42 Jun 24 '20 at 14:37
  • @andrewjames I think that your answer is much more than we thought to make it a duplicate, it might help jar beginners like me! I leave it and we see if someone wants to mark it as a duplicate with reference (you included) to https://stackoverflow.com/questions/50232557/visual-studio-code-java-extension-howto-add-jar-to-classpath which you mention yourself, thus we follow SO here, hopefully – questionto42 Jun 24 '20 at 16:08

1 Answers1

2

Make sure you have added the OpenCSV JAR file to VS Code first, regardless of what version of Java you are using (Oracle's or others). See details in the answers to this question for related information.

If you don't have the JAR for OpenCSV, you can download the latest version from Maven here. Maven also lets you search for JARs by names/keywords - see here.

In case you are not familiar with Maven (or Gradle): A next logical step (if you are going to be doing any amount of Java work) might be to consider using a more fully-featured Java IDE (e.g. Eclipse, NetBeans) and look at how those tools handle JARs via their Maven (or Gradle) project types. Instead of downloading individual JARs, you would add entries to the auto-created "pom.xml" file used by each of your Java projects. So, for OpenCSV, you would add this:

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

The huge advantage of this is that it automatically downloads any transitive dependencies into your project - i.e. any additional JAR files which may be needed by the JAR file that you need to use.

andrewJames
  • 19,570
  • 8
  • 19
  • 51