我完全坚持这个。我不太了解冷聚变,也无法找到答案,知道什么是错误的。
以下是我的coldfusion脚本中的一部分问题,在这里使用通用名称
<cfscript>
function run() {
var obj = createObject("java", "com.company.Run");
var variable1= "string1";
var variable2= "string2";
var variable3= "string3";
obj.init();
var success = obj.runThis(variable1, variable2, variable3); <-- failing here
return success;
}
</cfscript>
<cfset success = run()>
<cfoutput>SUCCESS? #success#</cfoutput>
这个访问的java代码在一个jar中,它被编译并适用于该类中的简单方法。例如,我可以使用coldfusion脚本下面的java代码执行此操作,它可以正常工作:
<cfscript>
function run() {
var obj = createObject("java", "com.company.Run");
var variable= "string";
obj.init();
var success = obj.print(variable); <-- failing here
return success;
}
</cfscript>
<cfset success = run()>
<cfoutput>SUCCESS? #success#</cfoutput>
public static String print(String input) {
return input;
}
我想要运行的是我的第一个冷融合脚本上面有这种想法,在这个类中运行&#34; runThis&#34;它从另一个类构造一个对象,然后从该对象运行一个返回字符串的方法。这些都在同一个jar中,所以当我对完全相同的代码运行junit测试时,应该都可访问并运行正常。这里的问题是将其拉入冷冻并在那里运行:
public class Run {
public Run() {
}
public String runThis(String variable1, String variable1, String variable3) throws Exception {
try {
MyObject object= new MyObject (variable1, variable2, variable3);
return object.execute();
} catch (Exception e) {
return "error";
}
}
}
我收到的错误:
"Error","http-bio-8500-exec-7","10/08/14","10:47:13",,"javax/ws/rs/WebApplicationException The specific sequence of files included or processed is: C:\ColdFusion11\cfusion\wwwroot\company\response.cfm, line: 77 "
java.lang.NoClassDefFoundError: javax/ws/rs/WebApplicationException
at com.company.Run.runThis(Run.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:97)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2483)
at cfresponse2ecfm1522673216$funcRUN.runFunction(C:\ColdFusion11\cfusion\wwwroot\company\response.cfm:77)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:487)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:383)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:95)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:334)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:231)
at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2840)
at cfresponse2ecfm1522673216.runPage(C:\ColdFusion11\cfusion\wwwroot\company\response.cfm:82)
at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246)
at coldfusion.tagext.lang.IncludeTag.handlePageInvoke(IncludeTag.java:734)
at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:570)
at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
at coldfusion.filter.IpFilter.invoke(IpFilter.java:45)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:487)
at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:42)
at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:141)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
at coldfusion.CfmServlet.service(CfmServlet.java:219)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at coldfusion.inspect.weinre.MobileDeviceDomInspectionFilter.doFilter(MobileDeviceDomInspectionFilter.java:121)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:422)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.WebApplicationException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
... 56 more
我感觉我没有正确地实例化MyObject对象,或者这是代码失败的地方。删除它似乎允许代码工作,但显然不会得到我想要的东西。
答案 0 :(得分:2)
进行这些Java集成的最佳方法是从实例化开始。所以首先运行你的创建代码并转储结果......你看到了什么?
<cfset obj = createObject("java", "com.company.Run")/>
<cfdump var="#obj#"/>
接下来转储init()函数(我在你的类中没有看到,但可能是超类的一部分):
<cfset obj = obj.init()/> // assuming it returns an instance of Run
<cfdump var="#obj#"/>
什么是转储显示你?你绝对应该将runThis视为该类的一种方法。
最后通过runThis方法完成工作。请记住,您传递的变量必须是Java字符串类型 - 这可能是您的问题。如果它们不是正确的类型,则args调用不同的方法签名。而不是像“我有一个runThis但这些不是正确的参数或类型”这样的有用信息“Java只是抛出”未找到的方法“(因为你可以通过传入不同的args来覆盖方法)。
如果字符串类型有问题,请尝试在方法调用中使用JavaCast(),如下所示:
var variable1= Javacast("String","string1");
以这种方式设置类型通常允许将数据作为正确类型在Java和CF之间的阈值上进行编组。
Java and CF上的这篇文章有点过时,但它有一些有用的方法。
希望这会有所帮助。
答案 1 :(得分:1)
我强烈怀疑你的jar缺少一个或多个依赖项。
我目前无法访问CF11,但您可以通过将项目导出到jar来重现类似的跟踪,但省略了一些必需的类。例如,我创建了两个类的虚拟版本(见下文)。请注意MyObject
引用第三个类 - YetAnotherClass
。然后我将所有内容导出到jar中,但故意排除YetAnotherClass
。
使用您的测试代码,我可以成功调用print()
。但是,当代码调用runThis()
时,会发生类似的错误。由NoClassDefFoundError
引起的ClassNotFoundException
- 因为我忽略了将其包含在jar中。
注意,我正在使用CF10进行测试。
CF代码
<cfscript>
obj = createObject("java", "com.company.Run");
result = obj.print("testing...");
writeDump(result);
obj.init();
result = obj.runThis("john", "doe", "atlanta");
writeDump(result);
</cfscript>
错误堆栈跟踪:
java.lang.NoClassDefFoundError: com/othercompany/YetAnotherClass
at com.company.MyObject.<init>(MyObject.java:10)
at com.company.Run.runThis(Run.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
......
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.othercompany.YetAnotherClass
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533)
生成强>
package com.company;
public class Run {
public Run() {
}
public String runThis(String var1, String var2, String var3) throws Exception {
MyObject object= new MyObject(var1, var2, var3);
return object.execute();
}
public static String print(String input) {
return input;
}
}
<强>为MyObject 强>
package com.company;
import java.util.Date;
import com.othercompany.YetAnotherClass;
public class MyObject {
private YetAnotherClass other;
public MyObject(String first, String last, String city) {
this.other = new YetAnotherClass();
}
public String execute() {
return "Method execute invoked at "+ new Date();
}
}
YetAnotherClass (不同的套餐)
package com.othercompany;
public class YetAnotherClass {
public YetAnotherClass() {
// do nothing to keep it simple
}
}