Skip to Content
Technical Articles
Author's profile photo Dhairya Khimsaria

How to use External Jar Files in SAP CPI

Introduction: –

While building an Integration in CPI, we often come across a situation where we need to use same piece of code which we have used earlier or sometimes we need to use external Jar file’s class to process some data.

This Blog will explain how you can use External Jar files in CPI and make use of different functions inside Jar file to process data. This Jar file can also be used as a common code repository where you can keep all the common functions which you usually use in CPI groovy script and then make use of it in different IFlows by simply importing it.

Problem Statement: –

How to reuse External Jar file’s functions in CPI to process Data.

Prerequisites: –

  1. You need Eclipse to create your own Jar file if you don’t have one
  2. You need basic understanding of groovy scripts in CPI. To know more about groovy scripting click here

Resolution: –

I will be explaining here from start how to create your own Jar file and then use it in CPI. If you already have your Jar file, then you can start following the blog from Step 8.

Step 1: – Open Eclipse and create a new project.


Step 2: – Select Java Project on the window and click on Next.


Step 3: – Give project name and select correct Java version.

So here you should give some logical name to your Project and make sure you select ‘Use an execution environment JRE’ radio button and ‘JavaSE – 1.8’ as an option. This is because CPI’s Java version is 8 and if you create Jar file on any other version it will give error while processing.

Step 4: – Click on Finish and you will be able to see your project on left hand side of Eclipse.


Step 5: – Create a new package under your Project. If you expand your project, you will be able to see something called as ‘src’. You need to right click on that folder and select New Package.

You need to give logical name to your package and then select on Finish.

Step 6: – Create a new Class under the new Package create in previous step. You need to right click on the newly created package and select new class.

Give a logical name to your class and select Finish. Please note that once you click on finish an empty class will be created and you can write your functions inside this class now.

For example, I have written two functions to concatenate Full name and to calculate Monthly salary of an employee. Please refer to the below screenshot.

Here you can write as many functions as you want and since this is a Java code you can also write complex Java functions here which can be used in CPI for processing.

Step 7: – After completing the code now you can export the project as Jar file, and it will be saved as a Jar file on your local machine.

Step 8: – Import this Jar file in your CPI IFlow. Go to Resources -> Add -> Archive

Now suppose we have the below xml payload. We want to concatenate the FullName and calculate monthly salary by using the functions in Jar file.

Input XML: –


We will be using mapping step to format the xml.

Now to use the functions from the Jar file, we need to use Groovy Script. Please refer to the below script which we have used here to access functions in Jar File.

import test.*;

def String FullName(String FirstName, String MiddleName, String LastName){
    CpiTest c1 = new CpiTest();
    String fullname  = c1.getFullName(FirstName,MiddleName,LastName);
    	return fullname; 

def String MonthlySalary(int AnnualSalary){
     CpiTest c1 = new CpiTest();
    def SalaryperMonth  = c1.getSalaryperMonth(AnnualSalary);
    	return SalaryperMonth; 

So, as you can see I have created two functions one to concatenate Full Name and one to calculate Monthly Salary. Both the functions use the Jar File Functions which we have imported. Please note to use any of the Jar File function in Groovy script we need to Import package which we have created while creating Jar File. This can be done by adding an import statement (import test.*).

So now when we use this groovy script and stimulate the above mapping, we get the desired result.

So, by using the Jar File function we got Full Name concatenated and Monthly Salary calculated as well.

We can use this Jar file in Groovy Script component as well. Below is the example where I have used Jar File function in normal groovy script.

I have used the same input xml as the incoming body to the groovy script and I have created CSV file out of it. Please refer to the below script which will convert the incoming XML into CSV file and will also use Jar file functions to do data transformations.

import java.util.HashMap;
import test.*;

def Message processData(Message message) {
    def body       = message.getBody(java.lang.String);
   def xmlBody    = new XmlSlurper().parseText(body);
    String FinalBody = "FullName,Salary\n";
        xmlBody.row.each { it ->
        String FN = it.FirstName.text();
        String MN = it.MiddleName.text();
        String LN = it.LastName.text();
        String Sal = it.AnnualSalary.text();
        int Salary = Sal.toInteger();
        CpiTest c1 = new CpiTest();
        def fullname  = c1.getFullName(FN,MN,LN);
        def SalaryperMonth  = c1.getSalaryperMonth(Salary);
        FinalBody = FinalBody + fullname + "," +SalaryperMonth + "\n";
    return message;

By using this Script, we get a CSV file with Headers as ‘FullName’ and ‘Salary’. We also convert the incoming XML data by using Jar File functions to populate the respective headers. Please refer to the below screenshot which shows the final CSV file data.

If you have a requirement where you need to process the data as an Array, then you can add a function in your Jar file which will accept input data as an Array. I have added a function which will accept AnnualSalary field data of all Employees as an Array and will add then all and send the total amount as an output. Please refer to the below screenshot.

New Function Added – CalTotalSalary. This Function will add all the integer data sent as an array and give total amount as an output. I used this function in CPI Mapping step by using the below mentioned script.

def void TotalSalary(int[] Salary, Output output){
     CpiTest c1 = new CpiTest();
    def Ans = c1.CalTotalSalary(Arrays.asList(Salary));

This is how you can modify the Jar functions as per your requirement and achieve the desired output.

Conclusion: –

This is how you can use Jar File functions in your CPI Artifact.

You can also follow the same process to create your own Jar File repository which you can use in all your Integrations. This will save a lot of time in terms of development of Integrations.

Hope this Blog post helps 😊



Dhairya Khimsaria

Senior Consultant, Veritas Prime Labs






Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Rajashekar Kuncham
      Rajashekar Kuncham

      Hi Dhairya,


      Excellent write-up. I am waiting for your next blog already !


      Shall stay tuned.



      Raja Sekhar

      Author's profile photo Deepali Garg
      Deepali Garg

      Great Content Dhairya! Thank you for Sharing 🙂

      Author's profile photo Suresh Reddy
      Suresh Reddy

      Good One Dhairya.

      Author's profile photo Suresh Reddy
      Suresh Reddy

      Good one Dhairya !

      Author's profile photo Shiva Prasad Narahari
      Shiva Prasad Narahari

      Good one Dhairya...!

      Author's profile photo Saketh Nethi
      Saketh Nethi

      That was really helpful and wonderful blog @dhairya. Keep up the good work

      Author's profile photo Martin Pankraz
      Martin Pankraz

      Hi Dhairya Khimsaria,

      always good to re-iterate topics like this. Have a look at what we did in the past blog posts. Section "Sharing scripts across iFlows" gives an additional angle to your contribution.



      Author's profile photo Daniel Zutzmann
      Daniel Zutzmann


      did you also try to use your custom external JAR within a xslt mapping?

      I am able to use functions of an external jar from apache in my xslt mapping, but my own functions fails as the class can't be loaded with error:

      Cannot find a 1-argument function named Q{java:test.FindEarliestDate}findEarliestDate(). Cannot load Java class java:test.FindEarliestDate. For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS

      The JAR is build accordingly to your guide and the class is part of the package "test".


      Snippet of the xslt mapping and class import:



      <xsl:stylesheet version="3.0" xmlns:xsl="" xmlns:a1="java:test.FindEarliestDate" exclude-result-prefixes="a1">

      Function call


      <xsl:variable name="scheduledPickupDate">
      <xsl:value-of select="a1:findEarliestDate(//ScheduledPickupDate)"/>



      Author's profile photo Daniel Zutzmann
      Daniel Zutzmann


      I used a newer version of eclipse IDE to create the jar and now the class/function also can be used within xslt mapping with the syntax:

      <xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:a1="java:test.FindEarliestDate" exclude-result-prefixes="a1">


      <xsl:variable name="scheduledPickupDate">
      <xsl:value-of select="a1:findEarliestDate(//ScheduledPickupDate)"/>




      Author's profile photo Sebastian Saieg
      Sebastian Saieg

      Great content, I really appreciate that