问题
I am using the following to initialize in CF 10 and I placed the xmlsec-1.5.8.jar file under Coldfusion10/cfusion/lib
<cfset Init = CreateObject("Java", "org.apache.xml.security.Init.Init()")>
I have also tried placing the above code in a cfscript as
Init = CreateObject("Java", "org.apache.xml.security.Init.Init()");
I am getting the following error.
You must initialize the xml-security library correctly before you use it. Call the static method "org.apache.xml.security.Init.init();" to do that before you use any functionality from that library.
Thank you.
回答1:
In addition to the syntax error, there is another problem. The error message is indicating you must call a static method of that class first. One that is literally named init(). The problem is init() has a special meaning in CF. CF does not allow using the new keyword with java objects. Instead, it uses the name init() as a pseudo-constructor, which allows you to create a new instance of a class. So when you do this:
obj = createObject("java", "org.apache.xml.security.Init").init();
CF will create a new instance of that class, NOT call a method named init(). AFAIK, the only way around it is to use reflection. Something along these lines:
// get a reference to the class
ref = createObject("java", "org.apache.xml.security.Init");
// initialize if needed
if (!ref.isInitialized()) {
// find static method named "init" with no parameters
method = ref.getClass().getDeclaredMethod("init", []);
// invoke it via reflection
method.invoke(ref, javacast("null", ""));
}
placed the xmlsec-1.5.8.jar file under Coldfusion10/cfusion/lib
While there is nothing wrong with doing that, as of CF10, you can also load jars dynamically via your Application settings, ie this.javaSettings. If the feature seems familiar, it is basically a rip of Mark Mandel's awesome JavaLoader.cfc, only baked into CF ;-)
来源:https://stackoverflow.com/questions/29480784/unable-to-correctly-initialize-xml-security-library-using-coldfusion-10-0