Gabriel
Gabriel

Reputation: 1838

Class not found even if it's imported

i have a class in my program i'm developing. It's the OpenCSV class for reading and handling csv files.

It's imported in my classpath in eclipse and also by import au.com.bytecode.opencsv.*;. Eclipse is not showing any errors and it fails on this Class.forName("au.com.bytecode.opencsv.CSVReader", false, null);

and i just can't get my head around why it fails on this line. I even tried to import all the source code into my project but it allways fails on ClassNotFound error. It's a jsp application running on apache tomcat 6 server.

Upvotes: 2

Views: 5468

Answers (4)

Vaibhav
Vaibhav

Reputation: 111

Try moving the csv jar file to src folder and then do the following: Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar

If you already have the csv jar file in src folder, try creating a new folder "lib" in the main project folder and add this csv jar file to it and follow the above steps.

Upvotes: 1

stjohnroe
stjohnroe

Reputation: 3206

Are the required classes in WEB-INF/lib or WEB-INF/classes of the deployed .war file?

Upvotes: 2

aioobe
aioobe

Reputation: 420921

You need to fully qualify the class name. Try

Class.forName("au.com.bytecode.opencsv.CSVReader", false, null);

From the docs of Class.forName:

Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface.

The import statement is used in compile-time only. (There is no trace of the import in the bytecode.) Thus the class loader that is asked to load a "CSVReader" can't know which package you're talking about (and actually just looks for the class in the default package).


Regarding your updates...

You need to make sure that the opencsv library is on the classpath for your web-application. It is not sufficient for it to be around during compilation...

Upvotes: 3

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43088

you should use FQCN(fully qualified class name) for Class.forName().

Upvotes: 0

Related Questions