5

How can I include jar/java files into my coldfusion project. currently I am using this line into me <cfscript>

Me directory is looks like this

website_name
         |__ Login
             |__ connection
                |__ display.cfm
                |__ twitter
                    |__ com
                        |__ ConfigurationBuilder.java

in my display.cfm file I am calling obj in script like this

<cfscript>
    configBuilder = createObject("java", "twitter.com.ConfigurationBuilder");
</cfscript>

but it gives me error on this line when I run display.com and error is this

An exception occurred while instantiating a Java object. The class must not be 
an interface or an abstract class. Error: ''. 

The error occurred in website_name/login/azam/connection/display.cfm: line 57


57 :            configBuilder = createObject("java", "twitter.com.ConfigurationBuilder");

How can I call my java class and how to call its function so that I can call my twitter function.

  • 1
    Did you add the jars to the class path *and* restart the server? Please [update your question](http://stackoverflow.com/posts/17381871/edit) to include the full error message **and** stack trace. – Leigh Jun 30 '13 at 19:35

4 Answers4

3

The easiest way to add class or jar files to your class path is to simply drop them in the lib directory where they are automatically picked up. The directory is located at {cf_installation}/servers/lib. These class files will be available to all servers.

http://blogs.adobe.com/cantrell/archives/2004/07/the_definitive.html

surfealokesea
  • 4,971
  • 4
  • 28
  • 38
  • 1
    That is the simplest option, but if you cannot add jars to the CF classpath, you can also use the [javaLoader.cfc](http://javaloader.riaforge.org/). – Leigh Jun 30 '13 at 19:34
  • @Leigh I used your method but when I run an error come and it says `Element CLASSLOADER is undefined in INSTANCE.` in `JavaLoader.cfc: line 97` and I am calling my object like this `` and then into my script i m calling this in my script like this `configBuilder = javaloader.create("ConfigurationBuilder").init(); ` –  Jul 01 '13 at 09:34
2

If you're using ColdFusion 10 you can use the newly built in feature to dynamically load Java Files:

Specifying custom Java library path in the Application.cfc

Leigh
  • 28,765
  • 10
  • 55
  • 103
BigMadKev
  • 2,440
  • 2
  • 17
  • 10
  • 1
    True, but their tags indicate they are using CF9. – Leigh Jun 30 '13 at 19:52
  • @Leigh when I createObjec of javaloader into localhost it works fine but when I do same and upload it on server and then run it then I got error that is `urlClassLoader = createObject("java", "java.net.URLClassLoader").init(urls);` the error is in `JavaLoader.cfc: line 390` –  Jul 01 '13 at 12:53
  • @user2166307 - 1) Do you have `createObject("java")` permission on the other server? It is required to use java objects. 2) Like I mentioned above **we need to see the stack trace** message. Is there a reason you have not posted it? :) – Leigh Jul 01 '13 at 15:11
  • 1) I have permission to create Java objects after mapping it 2) @Leigh what is **stack trace** sorry I don't know about it. –  Jul 01 '13 at 16:04
  • 1) Not sure what you mean by mapping. Java objects are not mapped. 2) The stack trace is at the very bottom of the blue error message box. You will see the text `"Stack Trace (click to expand)"`. – Leigh Jul 01 '13 at 16:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32692/discussion-between-leigh-and-user2166307) – Leigh Jul 01 '13 at 16:16
2

As surfealokesea said in their answer:

The easiest way to add class or jar files to your class path is to simply drop them in the lib directory where they are automatically picked up. The directory is located at

CF Directory(where CF is installed)
                             |__ servers/lib

These class files will be available to all servers.

You can also make your class files available only to the ColdFusion server by dropping them in

{cf_installation}/servers/default/cfmx/WEB-INF/lib.

(Note that putting them in {cf_installation}/servers/default/cfmx/WEB-INF/cfusion/lib will NOT work.)

Community
  • 1
  • 1
0

ColdFusion 9 here:

  1. Put jars into: wwwroot/WEB-INF/lib
  2. Put classes into: wwwroot/WEB-INF/classes (class mypackage.myClass goes into subdirectory mypackage)
Roland
  • 7,525
  • 13
  • 61
  • 124