How do I load jsoup Java library from inside Coldfusion 2016?

徘徊边缘 提交于 2019-12-11 00:09:58

问题


TLDR: CreateObject function throws an exception (java.lang.ClassNotFoundException) because it cannot see the java class/JAR file.

Any ideas what am I doing wrong? Thanks


contents of Application.cfc

<cfcomponent output="true">

    <cfset path = "#Mid(CGI.CF_TEMPLATE_PATH, 1, FindNoCase("index.cfm", CGI.CF_TEMPLATE_PATH)-2)#/java/lib" />
    <cfoutput>path: #path#</cfoutput>

    <cftry>
        <cfset This.javaSettings = {LoadPaths = ["#path#/", "#path#/java/lib/jsoup-1.12.1.jar", "./java/lib/", "./java/lib/jsoup-1.12.1.jar"], loadColdFusionClassPath = true, reloadOnChange = true}>

        <cfset jsoup = CreateObject("java", "org.jsoup.Jsoup") />

        <cfcatch type="any">
            <cfdump var="#cfcatch#" />
        </cfcatch>
    </cftry>

    <cfabort>
</cfcomponent>

JAR file location

/Volumes/mydrive/work/myapp/java/lib/jsoup-1.12.1.jar

output of the cfoutput inside Application.cfc

path: /Volumes/mydrive/work/myapp/java/lib

回答1:


@mrjayviper, I'm not sure why you have added the jsoup-1.12.1.jar in more times in loadPaths array ?

As well as your PATH = > /Volumes/mydrive/work/myapp/java/lib, But you have passed the value in loadPaths array like #path#/java/lib/jsoup-1.12.1.jar. Then it's should consider it as /Volumes/mydrive/work/myapp/java/lib/java/lib/jsoup-1.12.1.jar So it's totally wrong. That's an causes of your problem.

Here I wish to give some better solution for you with my sample application,

My simple application files structure look like this,

  • Application.cfc : Just normal Application.cfc file which having this.javaSettings to load the JSOUP jar file
  • index.cfm : Having code to fetching web page content using jSoup & executes the parsing operation
  • jsoup-1.8.3.jar : The downloaded JSOUP jar file

Then you can set the javaSetting in application.cfc file like below

component {
    this.name = "jSoupParser";
    //Loads the JAR File
    this.javaSettings = { loadPaths = [ "#expandPath('./jsoup-1.8.3.jar')#" ], 
                            reloadOnChange = false };
}

You could use the expandPath() and can point out your current application directory. Then you can create an object in your cfm file like below Index.cfm :

<cfset getJsoup = createObject("java", "org.jsoup.Jsoup")> 

I hope this help you more. If you want more explanation / clarification about this then please visit below link. Already my team gave the sample for this jSoup. https://www.mitrahsoft.com/index.cfm/blog/ColdFusion-Web-scraping-HTML-Parsing-using-JSOUP.




回答2:


Though technically there's nothing wrong with using absolute paths - you don't need them in this case. It's simpler to use a relative path, pointing to a subdirectory. For example: ./java/lib Also, don't include the same path multiple times.

<cfcomponent>
    <!--- don't forget to name the application ---> 
    <cfset This.name = "SomeUniqueApplicationName">
    <cfset This.javaSettings = { LoadPaths = ["./java/lib/jsoup-1.12.1.jar"] }>
</cfcomponent>

Then CF will be able to find the class when needed.

<cfset jsoup = CreateObject("java", "org.jsoup.Jsoup") />


来源:https://stackoverflow.com/questions/57968263/how-do-i-load-jsoup-java-library-from-inside-coldfusion-2016

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!