英文:
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("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();
}
}
}
Here are the exceptions I'm getting:
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
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论