为什么在使用OpenCSV时会出现’ClassNotFound’异常?

huangapple go评论79阅读模式
英文:

why do i get 'ClassNotFound' exception when using OpenCSV

问题

这是我翻译好的内容:

我正在尝试使用opencsv读取CSV文件我已经导入了所有内容并将库引用到了类路径中但是我一直在收到ClassNotFoundException错误

以下是我的代码

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import com.opencsv.CSVReader;

    public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        CSVReader readActors;
        try {
            readActors = new CSVReader(new FileReader(new File("src\\prueba\\actors.csv")));
                
        String[] values = null;
      
            try {
                while ((values = readActors.readNext()) != null){
                    String name =values[0];
                    String bday = values[1];
                    
                    System.out.println("Name:" +name+", bday:" +bday); //check if reading is ok
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
        
    }

以下是我收到的异常

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/ObjectUtils
    at com.opencsv.CSVParser.<init>(CSVParser.java:207)
    at com.opencsv.CSVReader.<init>(CSVReader.java:198)
    at com.opencsv.CSVReader.<init>(CSVReader.java:180)
    at com.opencsv.CSVReader.<init>(CSVReader.java:132)
    at com.opencsv.CSVReader.<init>(CSVReader.java:72)
    at prueba.Main.main(Main.java:20)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.ObjectUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 6 more

CSV文件的内容如下同样位于'prueba'包中以及Main类

    Margot Robbie,02/07/1990
    Leonardo DiCaprio,11/11/1974
    Brad Pitt,18/12/1963
英文:

I'm trying to read a CSV file using opencsv, I have imported everything and referenced the library in the classpath but i keep getting the ClassNotFoundException:

Here's my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import com.opencsv.CSVReader;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
CSVReader readActors;
try {
readActors = new CSVReader(new FileReader(new File(&quot;src\\prueba\\actors.csv&quot;)));
String[] values = null;
try {
while ((values = readActors.readNext()) != null){
String name =values[0];
String bday = values[1];
System.out.println(&quot;Name:&quot; +name+&quot;, bday:&quot; +bday); //check if reading is ok
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}	
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Here are the exceptions I'm getting:

Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/apache/commons/lang3/ObjectUtils
at com.opencsv.CSVParser.&lt;init&gt;(CSVParser.java:207)
at com.opencsv.CSVReader.&lt;init&gt;(CSVReader.java:198)
at com.opencsv.CSVReader.&lt;init&gt;(CSVReader.java:180)
at com.opencsv.CSVReader.&lt;init&gt;(CSVReader.java:132)
at com.opencsv.CSVReader.&lt;init&gt;(CSVReader.java:72)
at prueba.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.ObjectUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 6 more

and the csv file looks like this and is located in the package 'prueba' as well as the Main class.

Margot Robbie,02/07/1990
Leonardo DiCaprio,11/11/1974
Brad Pitt,18/12/1963

答案1

得分: 2

看起来您还需要将 org.apache.commons.lang3 的 JAR 包添加到类路径中。也许可以尝试访问这个网站:https://commons.apache.org/proper/commons-lang/download_lang.cgi

英文:

Looks like you also need to add org.apache.commons.lang3 jar to the classpath. Maybe try this site https://commons.apache.org/proper/commons-lang/download_lang.cgi

huangapple
  • 本文由 发表于 2020年4月7日 05:46:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61069458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定