DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How To Use the Node Docker Official Image
  • Creating Application using Spring Roo and Deploying on Google App Engine
  • Externalize Microservice Configuration With Spring Cloud Config
  • Implementing RBAC Configuration for Kubernetes Applications

Trending

  • Enforcing Architecture With ArchUnit in Java
  • The Future of Java and AI: Coding in 2025
  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks

Commanding the System With Mule

It's very easy to operate System commands through a Mule application using a Java class.

By 
Anirban Sen Chowdhary user avatar
Anirban Sen Chowdhary
·
Dec. 13, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
54.5K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, I wrote an article called (Data)Weaving Expressions in Java that demonstrated how we can execute a Dataweave expression language in Java.

This time, I thought of writing a different kind of article that would demonstrate a dynamic execution of system operations with a Mule application that we generally do in a system command prompt.

This article will demonstrate how we can dynamically pass a system command in a Mule application and get our job done.

We will be using a Java class in our Mule application to execute the System command as follows:

package com.service;

import java.io.*;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class SysCommand {

       private static Logger log = LogManager.getLogger(SysCommand.class);

       public String process(String command) {

              StringBuffer response=new StringBuffer();

              String line;

              try {

                     Process .getRuntime().exec("cmd /c "+command);

                     process.waitFor();

                     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                     BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));

                     while ((line = reader.readLine()) != null) {

                           response.append(line + "\n");

                           log.info(line);

                     }

                     while ((line = error.readLine()) != null) {

                           response.append(line + "\n");

                           log.error(line);

                     }

              } catch (IOException e1) {

              } catch (InterruptedException e2) {

              }

              return response.toString();

       }
}

Here, you can see we are passing the system commands dynamically through a variable and we are using the exec() method of the Runtime class to run the command as a separate process. Invoking the exec method will return a Process object for managing the subprocess.

Now, we design our Mule flow as follows:

  <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8087" doc:name="HTTP Listener Configuration"/>

    <flow name="systemappFlow">

      <http:listener config-ref="HTTP_Listener_Configuration" path="/execute" doc:name="HTTP"/>

      <object-to-string-transformer doc:name="Object to String"/>

      <component class="com.service.SysCommand" doc:name="Java"/>

      <logger message="Executed !" level="INFO" doc:name="Logger"/>

    </flow>

So, everything is set! We will be testing our application with few system commands.

Testing the Application

After we deploy our application on Mule server, we will hit the application from a REST client such as Postman with the URL http://localhost:8087/execute with a POST method:

Image title

Here, we can see if we pass the command dir as a payload body, we will get all the details of files and directory under present director which is the application directory.

Here's another example where we pass the ver command in our application and get the version of MS-DOS or Windows command prompt system:

Image title

Let’s take another example where we will be moving a file from one location to another. Let’s consider we have a file named abc.txt under the E:\temp folder as follows:

Image title

So, we will be moving this text file to another location (say, the E:\destination folder), so we will be using the following command in Postman for our Mule application as follows:

Image title

Now, if we go to the destination location, we will find the file has been moved into the destination folder:

Image title

We can open Notepad from our application:

Image title

If we want to execute any external script file such as a .bat file, we can do so easily:

Image title

By the same way, we can start our on-premises Mule runtime standalone server (or any other server) directly through our Mule application:

Image title

Testing Invalid Commands

Now, if we pass an invalid command to our application, our application is wise enough to detect it and display a response:

Image title

Conclusion

As you can see, it is very easy to operate System commands through a Mule application. We can execute different commands for different purposes starting from some kind of reporting or monitoring to get different system information to other purposes (such as executing different script files and bat files, as well as starting and maintaining different servers directly from this application itself).

application Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • How To Use the Node Docker Official Image
  • Creating Application using Spring Roo and Deploying on Google App Engine
  • Externalize Microservice Configuration With Spring Cloud Config
  • Implementing RBAC Configuration for Kubernetes Applications

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!