Mule 4 Mulesoft Basics Mulesoft Tutorial

Calling Custom Java Function in Mule 4

Calling Custom Java Function in Mule 4

In this tutorial we will learn how we can invoke custom java functions in mule 4. The ways by which Java functions can be called are:
1. Using Java Components
2. Dataweave
3. MEL script

Using Java Components:

Various Java Component to call custom java class:

  1. Invoke – Invokes a method on a provided instance.
  2. Invoke static – Invokes a static method on a provided class.
  3. New – Creates new instance
  4. Validate type – Verifies if a provided instance is an instance of a specified class.


First we will create a java class that will be invoked in mule code.

package com.mulejava;

public class StringManipulation {
  public String result;
  public StringManipulation() {
    // TODO Auto-generated constructor stub
  public void ConcatAB(String value1, String value2) {
    result = value1 + value2;	
  public static String staticFunctionTest() {
    return "static funtion invoked";	



[IMP] Then, we need to add the class package name in “mule-artifact.json” inside classLoaderModelLoaderDescriptor, so mule can identify class else “CLASS NOT FOUND” would be through while call the function.

  "configs": [
  "secureProperties": [],
  "redeploymentEnabled": true,
  "name": "mulejava",
  "minMuleVersion": "4.1.1",
  "requiredProduct": "MULE_EE",
  "classLoaderModelLoaderDescriptor": {
    "id": "mule",
    "attributes": {
      "exportedResources": [
  "bundleDescriptorLoader": {
    "id": "mule",
    "attributes": {}

Calling non-static function:

To call a non-static function we need to first create an instance of the class. We can do that by using “New” java component from mule palette.

Target Variable – Is the instance variable, will contain the object class returned from the constructor.


Now we will use Invoke java component to call the ConcatAB function.

<java:invoke doc:name="Invoke" doc:id="d864445c-6ce8-4b92-ada2-0ddbc7215ede" class="com.mulejava.StringManipulation" method="ConcatAB(String, String)" instance="#[vars.inst]">
  <java:args ><![CDATA[#[{
arg0: payload.a as String,
arg1: payload.b as String


Calling static function:

To call static function from the call we all need to use “Invoke Static” component from mule palette.


Calling Java Function in DW:

Only static variable can be call in DW:

<ee:transform doc:name="Transform Message" doc:id="4002c2b2-0cd5-438c-87ff-500b3b999752" >  
    <ee:message >    
      <ee:set-payload ><![CDATA[%dw 2.0
import java!com::mulejava::StringManipulation
output application/json
  a: StringManipulation::ConcatAAB(payload.a as String, payload.b as String)
}]]>			</ee:set-payload>


Calling Java Function in Set Payload:

First create instance to the java class that is to be invoked as we did above by using “New” java component from mule Palette.

Then inside set payload use Java invoke mule function to call the respective function by passing in:

  • Class Path
  • function name with datatypes of the arguments
  • Instance name
  • and Value to the arguments


<java:new class="com.mulejava.StringManipulation" target="inst" doc:name="Create Instance" constructor="StringManipulation()"/>
<set-payload value="#[Java::invoke('com.mulejava.StringManipulation', 'ConcatAB(String, String)', vars.inst, {arg0: &quot;tutorials&quot;, arg1: &quot;AtoZ&quot;})]" doc:name="Set Payload" doc:id="d0d8f6fb-f406-4783-9ca3-24e7b5461d61" />


Varun Goel

About Varun Goel

Varun Goel is a technology enthusiast with 6+ years exp in IT industry. In fact, he is been developing application after schooling as freelancer. Currently working with one of the Fortune’s 100 Companies having vast experience Mule ESB, Tibco, HTML5, CSS, JSS, Android, Core Java, JSP, PHP, MySQL, AutoCAD, Maya, ZBrush, Photoshop, Flash CS and many more.


Leave a Reply

Your email address will not be published.