I created this function to load the contents of th...
# cfml-general
g
I created this function to load the contents of the different website using jsoup
Copy code
<cffunction name="fetchDivContent" returnType="any" hint="Fetches and returns the content of a specific div">
        <cfargument name="targetUrl" type="string" required="yes" hint="The URL of the page containing the target div">
        <cfset var httpResponse = "" >
        <cfset var document = "" >
        <cfset var divContent = "" >
        <cftry>
            <!--- Send HTTP request to fetch the page content --->
            <cfhttp url="#arguments.targetUrl#" method="GET">
            <cfset httpResponse = cfhttp.FileContent>
            <!--- Parse the HTML content --->
            <cfset document = createObject("java", "org.jsoup.Jsoup").parse(httpResponse)>
            <!--- Find the <div> by its class name and extract its inner HTML --->
            <cfset divContent = document.select(".table-1").outerHtml()>
            <cfcatch type="any">
                <cfdump var="#cfcatch#">
                <!--- Log error if any occurs during fetching or parsing --->
                <cf_log type="error" file="#request.logs#/jsoupFailed.csv" text="#cfcatch.message#" time="#TimeFormat(now(),'long')#" date="#DateFormat(now(),'long')#">
            </cfcatch>
        </cftry>
        <cfreturn divContent>
    </cffunction>
but i am getting object initiation error i had setit tup in application.cfc as:
Copy code
this.javaSettings = {
        loadPaths               : [expandPath("./jars")],
        loadColdFusionClassPath : true,
        reloadOnChange          : false,
        watchInterval           : 100,
        watchExtensions         : "jar"
     };
and also verified that the jar files are in the right path
a
Good clear code example there. Can you pls also give the exact error message? Copy & paste the text.
g
Error Object Instantiation Exception. Class not found: org.jsoup.Jsoup The error occurred in C/ColdFusion2023/cfusion/wwwroot/ting/admin/cfc/dojo.cfc line 2707 Called from C/ColdFusion2023/cfusion/wwwroot/ting/schedule.cfm line 33 Called from C/ColdFusion2023/cfusion/wwwroot/ting/admin/cfc/dojo.cfc line 2707 Called from C/ColdFusion2023/cfusion/wwwroot/ting/schedule.cfm line 33
Copy code
2705 :         <cfset var document = "" >
2706 :         <cfset var divContent = "" >
2707 :         <cfset var jsoup = createObject("java","org.jsoup.Jsoup")>
2708 :         <cftry>
2709 :             <!--- Send HTTP request to fetch the page content --->
s
dump
expandPath("./jars")
just to make sure the files are there.
g
I did a dump and it’s rightly pointing to the path and the jar file
a
OK so if
loadPaths : [expandPath("./jars")],
didn't exist, you'd get a
The directory or file [whatevs] specified in LOADPATHS does not exist
error, so I'm guessing it's not that. I'd stick this in yer Application.cfc and see what it says:
Copy code
writeDump([
        dir = expandPath("./jars"),
        dirExists = directoryExists(expandPath("./jars")),
        files = directoryList(expandPath("./jars"))
    ])
Does the
files
bit list the jsoup jar?
NB: this all works fine for me on my rig, so the problem is def without your code or environment.
g
image.png
a
The only thing different in my code is I used a full path, rather than a relative one (eg:
/jars
rather than
./jars
). If CF loads the file at runtime, when the
createObject
is called, that path might be (incorrectly) interpreted as being relative to the file with the
createObject
call, not relative to Application.cfc's location?
g
I can try that but I do not see it will have a difference
a
Well do you want to put a small amount of effort in to solving the problem, or do you want to stick with non-working code and "not see it will have a difference"
g
i changed as you said, but still same error Object Instantiation Exception. Class not found: org.jsoup.Jsoup
image.png
s
may be some issue with jsoup jar file, try to download latest jsoup version and test.
g
its the latest file
a
OK can you put a
throw "yup";
in the Application.cfc just before the
javaSettings
, to demonstrate you have that config in the same Application.cfc that's being executed for the code yer running?