How to use Java Methods with Caché?

1.2k views Asked by At

I recently read that it is possible to use Java methods in a class by using the language attribute.

I tried this:

Method JavaTest() As %String [ Language = java, WebMethod ]
{
    string tmp = "Hello World";
    return tmp;
}

The code compiles, but when the method is invoked it returns an error:

ERROR #5002: Cache error: <METHOD DOES NOT EXIST>
2

There are 2 answers

1
psr On BEST ANSWER

As far as I can tell, this facility exists for when you use the %Projection.Java class to create a Java class from Cache. At any rate, if you do use a Java projection then the method does actually get projected to the Java class that is created.

Class SomePackage.JavaTest Extends %Persistent
{

Method JavaTestyTest() As %String [ Language = java, WebMethod ]
{
    string tmp = "Hello World";
    return tmp;
}
Method bleh() as %Library.String
{
    q "bleh"
}
Projection NewProjection1 As %Projection.Java(ROOTDIR = "C:\trans");

}

generates too much Java to show all of it, but it includes

public java.lang.String JavaTestyTest () {
       string tmp = "Hello World";
       return tmp;
}

and

public java.lang.String bleh () throws com.intersys.objects.CacheException {
    com.intersys.cache.Dataholder[] args = new com.intersys.cache.Dataholder[0];
    com.intersys.cache.Dataholder res=mInternal.runInstanceMethod("bleh",args,com.intersys.objects.Database.RET_PRIM);
    return res.getString();
}
1
herman_slagman On

Actually the %Projection classes are for projecting Cache class to Java. They generate Java proxy classes which you can use in Java projects.

In order to use Java classes and methods you need the Java Gateway which is a part of Ensemble and AFAIK not of Cache.