如何从Coldfusion 2016内部加载jsoup Java库? [英] How do I load jsoup Java library from inside Coldfusion 2016?
问题描述
TLDR:CreateObject函数引发异常(java.lang.ClassNotFoundException),因为它看不到Java类/JAR文件.
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
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文件位置
/Volumes/mydrive/work/myapp/java/lib/jsoup-1.12.1.jar
Application.cfc内cfoutput的输出
path: /Volumes/mydrive/work/myapp/java/lib
推荐答案
@mrjayviper,我不确定为什么您在loadPaths数组中多次添加了 jsoup-1.12.1.jar ?
@mrjayviper, I'm not sure why you have added the jsoup-1.12.1.jar in more times in loadPaths array ?
和您的PATH = > /Volumes/mydrive/work/myapp/java/lib
一样,但是您已经像#path#/java/lib/jsoup-1.12.1.jar.
这样在loadPaths数组中传递了值,因此应该将其视为/Volumes/mydrive/work/myapp/java/lib/java/lib/jsoup-1.12.1.jar
,所以这是完全错误的.这是造成您问题的原因.
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:只是具有以下内容的普通Application.cfc文件 this.javaSettings加载JSOUP jar文件
- index.cfm:具有使用jSoup&执行解析操作
- jsoup-1.8.3.jar:下载的JSOUP jar文件
- 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
然后,您可以在 application.cfc 文件中设置JavaSetting,如下所示:
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 };
}
您可以使用expandPath()并指出当前的应用程序目录.
然后,您可以在cfm文件中创建一个对象,如下所示
Index.cfm :
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")>
希望这对您有所帮助.如果您想对此进行更多的解释/说明,请访问下面的链接.我的团队已经为此jSoup提供了示例. https://www.mitrahsoft. com/index.cfm/blog/ColdFusion-Web-scraping-HTML-Parsing-using-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.
这篇关于如何从Coldfusion 2016内部加载jsoup Java库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!