This tutorial will show you an example on starting mule apps from main class. You can use any IDE for building your mule apps. Here we will see how to build and start mule apps in Eclipse. You can also use Mule or Anypoint Studio to build your mule apps.

In our previous tutorial we have seen an example on building mule apps with Gradle in Eclipse. Here we will use the previous tutorial to show example on starting mule apps from main class. So basically if you don’t have Anypoint or Mule studio then also you can build your mule apps outside Mule or Anypoint studio.

To start you Mule flow or app from Java, you need to refer Mule Context.

As we know Spring combines all configuration files into single ApplicationContext, similarly Mule also combines all resource files or configuration files into single ApplicationContext. The configuration files may be Spring configurations or Mule configurations.

So let’s first concentrate on our previous example on building mule apps with Gradle in Eclipse.

Open the file mule-config.xml under src/main/app directory. In this file notice that the values for host and port are referring using EL (Expression Language) and these values are coming from src/main/app/ file but this file has not been imported into the mule-config.xml file.

Therefore when you run the mule apps from main class you will get exception.

So let’s fix the issue first – import the file into mule-config.xml file as shown below.

starting mule apps from main class


Please make sure you have the entries as highlighted in the above mage.

Here in the above image you see <set-payload/> component after <http:inbound-endpoint/> which will send the payload and display on browser.

The corresponding should have the following entries

starting mule apps from main class

Now let’s create a main class that will run the Mule server or Mule application:

package com.jeejava.mule.server;

import java.util.logging.Logger;

import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.config.MuleConfiguration;
import org.mule.api.context.MuleContextBuilder;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;

public class RunMuleServer {

	static final Logger LOGGER = Logger.getLogger(RunMuleServer.class.getName());

	public static void main(String[] args) throws MuleException {"Starting Mule Server...");
		MuleConfiguration muleConfig = new DefaultMuleConfiguration();
		MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
		MuleContextFactory contextFactory = new DefaultMuleContextFactory();
		SpringXmlConfigurationBuilder configurationBuilder = new SpringXmlConfigurationBuilder(
				new String[] { "mule-config.xml" });
		MuleContext muleContext = contextFactory.createMuleContext(configurationBuilder, contextBuilder);
		muleContext.start();"Mule Server started");


In the above main method you see we are using MuleContext in order to start the application.

Notice how we have loaded mule-config.xml configuration file using the following code:

SpringXmlConfigurationBuilder configurationBuilder = new SpringXmlConfigurationBuilder(
				new String[] { "mule-config.xml" });

You may also use the below code when you have to load just one configuration file

SpringXmlConfigurationBuilder configurationBuilder = new SpringXmlConfigurationBuilder("mule-config.xml");

We have finished with our required coding part. Now let’s start the main class by clicking Run As -> Java Application and check the console log as follows:

starting mule apps from main class

Testing the application

Now test the application by hitting the URL http://localhost:8081 and you should see the below output on the browser.

starting mule apps from main class

You have successfully completed the example on starting mule apps from main class. Now you can add new flows, components etc. to the Mule apps.

Thanks for reading.


I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *