How do you use java files in Coldfusion

Chimeara picture Chimeara · May 29, 2014 · Viewed 8.7k times · Source

I need to import a java file into a coldfusion 8 page e.g. :

public class Hello 
{ 
    public String testJava() 
    { 
    return "Hello Java!!"; 
    } 
}

In Coldfusion I have the following code:

<cfscript>
   helloWorld = CreateObject("java","Hello");
   helloTest = helloWorld.testJava();
</cfscript>

I then receive the error

Object Instantiation Exception. Class not found: Hello

In my Coldfusion server Java Virtual Machine Path is set to 'C:/ColdFusion8/runtime/jre', So this is where I have put my java file, is this correct? Should I put a .java, .class or .jar there?

Does the file name need to be consistent with class name?

Does anyone have working sample code for something similar I can try?

Answer

Adam Cameron picture Adam Cameron · May 29, 2014

You need to put the files on the ColdFusion JVM's classpath, not in its JRE dir.

As a rule, if you have a jar file, put it in the instances's WEB-INF/lib dir, if it's just a class, put it in the WEB-INF/classes dir, eg: for me the latter would be C:\apps\adobe\ColdFusion\11\full\cfusion\wwwroot\WEB-INF\classes, where C:\apps\adobe\ColdFusion\11\full\ is where I installed CF, and cfusion is the name of the instance.