ColdFusion/Java Class not found Exception
Asked Answered
C

1

6

I am trying to parse a CSV File with Coldfusion and a JavaLibrary. I found some examples but it seems that ColdFusion cannot find the Jar-File.

This is my Code:

<cfset t01= getTickCount()>
    <cfscript>
        fileReader = createobject("java","java.io.FileReader");
        fileReader.init("C:\Dev\files.csv");
        csvReader = createObject("java","au.com.bytecode.opencsv.CSVReader");
        csvReader.init(fileReader, ",");
    </cfscript>

<cfset t02= getTickCount()>
<cfset ArrayData = csvReader.readAll()>
<cfset t03= getTickCount()>

<cfoutput>
    Process Data: #t02 - t01# ms
    Display Dump: #t03 - t02# ms
    <cfdump var="ArrayData"><cfabort />
</cfoutput>

and this is the ErrorMessage:

java.lang.ClassNotFoundException: au.com.bytecode.opencsv.CSVReader
    at coldfusion.bootstrap.BootstrapClassLoader.loadClass(BootstrapClassLoader.java:235)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248).....more Stack blabla......

I am using the opencsv Library. The Jar File is in the following folder:

wwwroot/WEB-INF/lib

I also restarted the Server multiple times.

Thanks for your help!

Commit answered 17/4, 2015 at 5:29 Comment(0)
S
2

I downloaded the opencsv jar file and it looks like you might be referencing it incorrectly.

Instead of this:

csvReader = createObject("java","au.com.bytecode.opencsv.CSVReader");

Try this:

csvReader = createObject("java","com.opencsv.CSVReader");

I was looking at the latest version, 3.3, but I assume that hasn't changed.

From the comments (my assumption was incorrect)

As Leigh pointed out in the comments, older versions of the opencsv library used a different package name than the latest version does. Old versions used au.com.bytecode.opencsv but the new versions use com.opencsv.

Starstarboard answered 17/4, 2015 at 13:43 Comment(2)
@Commit - I suspect Miguel-F hit the nail on the head. Looking at the source, older versions (2.2 for example) used a different package name ie au.com.bytecode.opencsv. In newer versions the package name was switched to com.opencsv. You are probably using one of the newer jars, but with the old package name. Hence why it is not found. The above should solve the problem.Alejandro
How to run same snippet in "ram:///"? I was stuck with it. Seems fileReader can not process in-memory path.Disserve

© 2022 - 2024 — McMap. All rights reserved.