开发者

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.


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...


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜