1

I took the java implementation of the Factual API (reference http://developer.factual.com/) and made a JAR file for factual. I did this by opening a new project in eclipse with the factual java files and then exporting to a new jar file.

I put that jar file in my coldfusion installation's /WEB-INF/lib/ folder.

After restarting Coldfusion, I tried to create a new cfobject like so

<cfscript>

     // Initialize the Java class. 
     factualClass=CreateObject("java", "src.main.java.com.factual.driver.Factual"); 

</cfscript>

I get an error indicating that it cannot find the Factual class.

Can anybody give me some guidance?

  • No, jars can be in either location. Personally I prefer to use /web-inf/lib and leave cfusion\lib for built in jars only, but either directory would work, as long as you restart afterward. @user2320726 - Which version are you using? CF10+ supports dynamic loading via the application.cfc, http://stackoverflow.com/questions/32001030/google-java-api-conflicted-with-coldfusion-cfhttp/32016842#32016842 – Leigh Mar 29 '16 at 18:55
  • CF11 ... Could I have got it wrong somehow when I made the jar file? I can look at the jar structure via 7-zip file manager, and it looks like the path is okay that way. – user2320726 Mar 29 '16 at 19:31
  • Either that or your createObject path is off. If you look at one of the classes, the base package is [`com.factual.driver.*`](https://github.com/Factual/factual-java-driver/blob/master/src/main/java/com/factual/driver/Boost.java). That is the structure that should be in your jar file, and your createObject statement. What are the top level folders in your jar? Side note, while what you are doing works fine as well, in CF11 it is easier to use application level class loading. No server restart required. See the Application.cfc link above. – Leigh Mar 29 '16 at 20:07
  • I tried changing the path to "com.factual.driver.Factual" without success. When I look at the jar file structure it mimics the path that I see when I open the zip file that I downloaded with all the java files in it. Should I re-create the jar file so that it starts at "com" level ? – user2320726 Mar 29 '16 at 20:51
  • It is usually simpler to build with Maven. Here's a precompiled jar (not sure if it is the right version). http://mvnrepository.com/artifact/com.factual/factual-java-driver/1.8.9 . Notice the top level folder is `com.*`? That is what it should be, then your createObject call would use `com.factual.driver.Factual`. – Leigh Mar 29 '16 at 21:49
  • [Did you select export "generated class files..."?](http://stackoverflow.com/questions/423938/java-export-to-an-jar-file-in-eclipse) – Leigh Mar 29 '16 at 21:55
  • I didn't export generated class files, and that is what was wrong with it. Thank you for your help. I'm pretty sure that the one you pointed me to will work, and if not, all I need to do is to compile it and export to a jar again. Thank you for your help and patience. :) – user2320726 Mar 30 '16 at 12:45

1 Answers1

0

(Summary from comments)

It sounds like you may be exporting the source files ie *.java rather than the compiled class files, ie *.class. In the Jar Export wizard, be sure to select the "Export generated class files and resources" option. (To automatically compile the project sources before expi, enable the setting: JAR packaging > Build projects if not build automatically option). If you prefer you can also find pre-compiled jars in the MVN repository.

put that jar file in my coldfusion installation's /WEB-INF/lib/ folder.

CF10+ also supports dynamic class loading via a new application level setting THIS.javaSettings.

 // Initialize the Java class. 
 factualClass=CreateObject("java", "src.main.java.com.factual.driver.Factual");

Just as a point of interest, src/main/java/ is not actually part of the libary class name. It is a standard directory structure used in Maven projects. It is probably included when exporting the sources, but not the compiled classes.

You can always verify the correct path and class name either by examining the API ie javadocs or by viewing one the source files. Package declarations are always at the top of the source file, such as on line 1 of src/main/java/com/factual/driver/Factual.java:

     package com.factual.driver; // ie "com.factual.driver"

.. and the class declaration on line 39.

     public class Factual {   // ie "Factual"

Combined that gives you the exact (case-sensitive) path to use with createObject:

     factualClass=CreateObject("java", "com.factual.driver.Factual");
Community
  • 1
  • 1
Leigh
  • 28,424
  • 10
  • 49
  • 96