Back to Blog Archive

Using your Java code in Mule4

Posted on: January 8, 2019

After the big success of the first part of using Java in Mule, finally, the second volume is ready, using Java in Mule4.

In this post, we take a look at different methods of integrating your Java code in the latest version of Mule.

The main difference to notice is that MEL is gone. In Mule3, it was a normal approach to call a Java method by providing a MEL expression with the fully-qualified name of a class, for instance: #[]

This is no longer valid as MEL has been replaced with Dataweave. Instead, we have a new Java Module ready for us, sorry I forgot to add out-of-the-box Java module.

Let’s start playing around.


Using the Java module component

Calling a Java Static class

For this example, we use a simple utility class as shown:

Let’s execute the returnTestMessage() using the Java module:

Creating a new Instance of a class

For this example, we create an instance of an Animal POJO class (sounds familiar? 🙂 ).

To create a new instance, simply use the “New” operation of the Java module with the default constructor “Animal()”.

Providing argument values to constructors and methods

I am guessing that this question is in your mind, how do you pass values using the Java Module?

Quite simple, within the “args” section, we must construct a map of arguments (using Dataweave) required for a constructor or method and also, specify the type of the arguments.

By default, arguments must be provided in sequence (arg0, arg1, arg2…) matching the method signature. If we want to reference the parameters by name, we need to provide the  -parameter flag to the compiler. In my case, I am using the maven compiler plugin:

Now it is possible to replace the “argX” key names with the actual field name. In this example, “arg0” can be replaced with “name”.

Calling a method of an existing instance of a class

By using the “Invoke” operation of the Java module, we can call any method of an existing instance:

Notice that an instance is provided to the “invoke” method.


Using Java via Dataweave

This is one of the best new features of Mule4, the usage of Java inside Dataweave. Mule4 and Dataweave 2 allows you to import Java classes and to call Java methods directly in the Java module.

Importing a class in Dataweave

Use the “java!” prefix to import a java class replacing the ‘.’ of your fully-qualified name with ‘::’

Note: this approach can only be used to call static methods or constructors, not to call instance methods.

Calling the Java module via Dataweave

We can access the Java module by calling “Java::invoke” syntax. This syntax receives up to 4 parameters:
– fully-qualified name
– method signature
– an instance of a class
– additional parameters as an object

Using Spring in Mule

In order to use Spring, import the Spring module from the palette into your application. In this example, I defined an XML bean of the “Animal” class:

Let’s inject it into the registry by using the Spring Config global element:

Now we can access our Animal object by using the expression  “app.registry.defaultAnimal”



Contact Us

Ricston Ltd.
Triq G.F. Agius De Soldanis,
Birkirkara, BKR 4850,
MT: +356 2133 4457
UK: +44 (0)2071935107

Send our experts a message

Need Help?
Ask our Experts!