12

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?

Chimeara
  • 695
  • 7
  • 27

3 Answers3

13

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.

SirDerpington
  • 11,260
  • 4
  • 49
  • 55
Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • 2
    As just an FYI, if you ever move up to CF10 or higher, you can also use this.javasettings to specify a folder for CF to find JARs. Makes it a bit easier (normally). You can also use the open source project, JavaLoader, to do the same thing. Not sure if it works in 8, but I'm sure it works in 9. – Raymond Camden May 29 '14 at 12:09
7

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

Do you mean the name of the .java file? Yes, but that is a java requirement. If your class is named "Hello", then your source file must be named "Hello.java" or it will not compile. When you compile the source code, the java compiler will generate a file named "Hello.class". Copy that file into the CF class path. The convention is to place individual .class files inside WEB-INF\classes and jar files inside WEB-INF\lib, as Adam mentioned. Note, you must restart the CF server for it to detect the new class(es)

After restarting, you will be able to create an instance of the class. However, be sure to use the correct class name in your createObject statement. It should be "Hello" - not "Test". Also, unlike most things in CF, the class name is cAsE sEnSiTive.

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


Update: That said, using individual class files is fine for testing purposes. However, you would normally package classes into a .jar file instead. With jar files, the file name is irrelevant. Only the internal path to the class matters. If your class resides in a package, you must include the package name as well. For example, if your Hello class resides in a package named com.utilities, the full path would be:

 helloTest  = createObject("java", "com.utilities.Hello");

In terms of the class path, usage is the same. Just place the jar file in the CF class path ie WEB-INF\lib and restart. As Raymond mentioned in the comments, for CF10+ see Specifying custom Java library path in the Application.cfc without dynamic loading.

Community
  • 1
  • 1
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • Thanks! So if I use a .jar file can I use any name for the jar? How do i reference it in CF?: helloWorld = CreateObject("java",classname); helloWorld = CreateObject("java",jarname); helloWorld = CreateObject("java",jarname.classname); – Chimeara May 29 '14 at 23:46
  • Ahh I think it should be CreateObject("java",packagename.classname); – Chimeara May 30 '14 at 01:28
0

You really should concider to use JavaLoader from Mark Mandel. It will make your life easier :-) https://github.com/markmandel/JavaLoader

GunterO
  • 389
  • 2
  • 13