cancel
Showing results for 
Search instead for 
Did you mean: 

Using External Jars in CPI Groovy Script

sugata_bagchi2
Active Contributor
0 Kudos

Hello Experts,

I am currently working on Amazon SP-API integration and developed a Java class to handle the AWS4 signature.

I am trying consume this java class as an external jar in a groovy script and call it's methods.

There is an option to upload the Jar files in the Iflow resources, but when I am referring the custom java class package in the local groovy script it is giving error.

I am wondering how in CF env. / WebUI, I can refer the external Jars inside my Groovy scripts.

Request your help and advice.

Thanks

Sugata

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor
0 Kudos

Hi Sugata

What you are doing is correct. You didn't mention which specific error you are getting, but I have two guesses:

  1. Remember to either import the class at the top of your script or refer to it using its fully qualified class name (i.e. including the package)
  2. Make sure that the class runs on Java 8, which is CPI's Java version

As for that old blog post of mine, please read the first paragraph again 😄

Regards,

Morten

sugata_bagchi2
Active Contributor
0 Kudos

Hello Morten,

Thank you for your reply! Yes, I have followed the same-

1. compiled the java class using jdk 8

2. referred to the full path of the package name in the groovy script

3. uploaded the external jar in the local reference of Iflow.

Here is the script -

import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.javagroovy; // external Java class package
import java.util.HashMap;
import src.main.resources.lib.*; // thought of adding this as jars are uploaded into this
def Message processData(Message message) {
    //Body 
       JavaGroovy jg = new JavaGroovy();
       def ret = jg.addinGroovy(3,12);
       message.setBody(ret);
       return message;
}

Here is the error log -
Error Details

Error Details



javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1__Script.groovy: 19: unable to resolve class com.sap.javagroovy
 @ line 19, column 1.
   import com.sap.javagroovy;
   ^

script1__Script.groovy: 24: unable to resolve class JavaGroovy 
 @ line 24, column 19.
          JavaGroovy jg = new JavaGroovy();
                     ^

script1__Script.groovy: 24: unable to resolve class JavaGroovy 
 @ line 24, column 24.
          JavaGroovy jg = new JavaGroovy();
                          ^

3 errors
, cause: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1__Script.groovy: 19: unable to resolve class com.sap.javagroovy
 @ line 19, column 1.
   import com.sap.javagroovy;
   ^

script1__Script.groovy: 24: unable to resolve class JavaGroovy 
 @ line 24, column 19.
          JavaGroovy jg = new JavaGroovy();
                     ^

script1__Script.groovy: 24: unable to resolve class JavaGroovy 
...





Thanks

Sugata

MortenWittrock
Active Contributor
0 Kudos

Hi sugata.bagchi2

The error message is fairly clear: There is no class called com.sap.javagroovy. If the class is actually called JavaGroovy, specify that. If javagroovy is a package, and you want to import all classes in that package, use: import com.sap.javagroovy.*

Also, put your classes in a package of your own rather than com.sap.

Regards,

Morten

sugata_bagchi2
Active Contributor
0 Kudos

Thank you Morten.

My Bad! not sure how have I missed the *.

I have updated the code, but still getting the error-

Here is my external Java class -

and below is my updated GroovyScript-

import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.javagroovy.*;
import java.util.HashMap;
//import src.main.resources.lib.*;
def Message processData(Message message) {
    //Body 
       JavaGroovy jg = new JavaGroovy();
       def ret = jg.addinGroovy(3,12);
       message.setBody(ret);
       return message;
}

But still getting the below error -

Error Details



javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1__Script.groovy: 24: unable to resolve class JavaGroovy 
 @ line 24, column 19.
          JavaGroovy jg = new JavaGroovy();
                     ^

script1__Script.groovy: 24: unable to resolve class JavaGroovy 
 @ line 24, column 24.
          JavaGroovy jg = new JavaGroovy();
                          ^

2 errors
, cause: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1__Script.groovy: 24: unable to resolve class JavaGroovy 
 @ line 24, column 19.
          JavaGroovy jg = new JavaGroovy();
                     ^

script1__Script.groovy: 24: unable to resolve class JavaGroovy 
 @ line 24, column 24.
          JavaGroovy jg = new JavaGroovy();
                          ^ 





MortenWittrock
Active Contributor
0 Kudos

Hi sugata.bagchi2

Are you sure you've packaged the JAR correctly? It should contain a directory called com, with a subdirectory called sap, with another subdirectory called javagroovy, which then contains the JavaGroovy.class compiled class.

So:

com\
sap\
javagroovy\
JavaGroovy.class

If the JAR is correctly packaged, please try renaming your package to, say, sugata.javagroovy or something similar. I.e. not com.sap. You should do that anyway, since your code doesn't belong in a subpackage of com.sap.

Regards,

Morten

sugata_bagchi2
Active Contributor

Hello Morten!

You are the best!
After changing the package name to a custom one, it worked. But this is something unusual.

Thank you so much for your help.

Thanks

Sugata

Answers (1)

Answers (1)

Karunaharan
Product and Topic Expert
Product and Topic Expert
0 Kudos
sugata_bagchi2
Active Contributor
0 Kudos

Hi Karunaharan,

I went through the links from Help SAP you provided.

But, they are not addressing my requirements.

please have look into this blog, which says about this using eclipse tool not the webui or cf.

https://blogs.sap.com/2017/01/23/how-to-use-external-java-libraries-in-hci/

My requirement is to do the same using CF env.

Thanks

Sugata