web stats

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

How to invoke a Java class from a JS connector.

This mini guide aims to explain the FAQ about guide will show you how to invoke create a custom Java class from Mirth. It , compile/package it into a JAR, include it in Mirth Connect, and invoke it in JavaScript. Your custom code can be invoked from any connector (Preprocessor, Postprocessor, JS Transformer, JS Filter, JS Reader, JS Writer), the only restriction is that this connector must support JS.

As this mini guide is a remake of some (old) forum posts, I will use the same examples.


Development environment working properly. Take a look to the eclipse guide (broken link)http://www.mirthproject.org/index.php?option=com_jd-wiki&Itemid=&id=eclipse for configuring eclipse, but this configuration is optional.JavaScript context, including the global/channel scripts, filters/transformers, and JavaScript connectors.


A working development environment, including the JDK (Java Development Kit). Generally you should use the same version of Java that your Mirth Connect server is using.

You can look here to see how to set up Eclipse to checkout and develop Mirth Connect, but it's an optional step. For this example we'll just use a single .java file and a command-line terminal.

Creating the Java code

First step is (obviously) create the classes create the class(es) you want to call to invoke from Mirth . There is no need to import some packages from Mirth's libs, extend from Connect. You may import internal Mirth Connect packages if you need to extend some class or implement any interface. A simple POJO will do the job.

In my example I will use this class:


some interface (see here for an example of extending AcceptMessage for a custom Web Service Listener), but you certainly don't need to. This example will just use a simple POJO called JarTest:

Code Block
package com.example;

public class JarTest {

    public JarTestString returnSomething() {
        System.out.println("hello world");

    public void Repeat() {
        System.out.println("I said: hello world")return "Hello, World!";


Note that it must have the package name declared. You can implement your code in a single class or using many classes.

Packing your code

Now let's pack our code into a jar file. Don't forget any class you need into the jar. In eclipse you can export as a jar file using rigth-click on the classes, export, jar file.

Installing and testing


Using a package directory structure

Put your newly created JarTest.java file in the following structure:

  • src
    • com
      • example
        • JarTest.java
  • classes

Note how there are two top-level folders, src and classes. The src folder will contain your source files (.java), and the classes folder will contain your compiled class files (.class).

Compiling your code

Now you need to convert your source code into compiled class files, which will be included in the JAR. In a command-line terminal, navigate to the top-level folder that contains your src and classes folders. Then do this:

Code Block
javac -d classes src/com/example/*

If you need to include other libraries in the classpath, use the -cp option. Type "javac -help" for the full set of options.

Creating the JAR file

Now you should have JarTest.class in the classes/com/example folder, so the next step is to archive it into a JAR file. In the same top-level directory as before, do this:

Code Block
jar -cf JarTest.jar -C classes com

The -c option means you're creating a new archive, and -f specifies the filename to output with. The -C option changes the working directory before adding files. Finally, the "com" at the end indicates what files/folders to include in the archive.

You can also compile source files and create a JAR in one step in Eclipse, with the export feature.

Installing and testing

You will be able to use the custom class anywhere JavaScript is used. For example, you can create a new channel that uses a JavaScript Writer destination, and use the following code:

Code Block
var obj = new Packages.com.example.JarTest();

At first this channel won't yet work; we need to include the JAR that we just created.

Mirth Connect 3.1.1 or earlier

    • Drop the JAR file into the lib/custom folder (for version 1.8.2 or earlier) or custom-lib (for
  • mirth connect >=
    • versions 2.0.0 through 3.1.1) under your Mirth Connect home directory. This way, the classloader should be able to locate it.
    • Restart the Mirth
  • and log in.
  • Define a test channel. I.E: channel reader, channel writer and JS Transformer step.
  • In the JS Transformer put a code like:

    Code Block
    var ud = new Packages.org.azufre.test.JarTest();
  • Now, send a dummy message throght the testing channel. If all the setup is fine, Mirth logs must show something like:

    Code Block
    INFO | jvm 1 | 2008/10/28 17:34:31 | hello world
    INFO | jvm 1 | 2008/10/28 17:34:31 | I said: hello world
    INFO | jvm 1 | 2008/10/28 17:34:36 | hello world
    INFO | jvm 1 | 2008/10/28 17:34:36 | I said: hello world


  • I've tested passing args from JS to Java and retrieving them from Java methods in JS code. For simple type like int, String ... etc is straightforward, for complex types I didn't tested yet, but I suppose that the solution is to create POJO's and use accessor methods to manage only simple types.
    • Connect service, and login to the Administrator.

Mirth Connect 3.2.0 or later

    • In 3.2 or later you can define custom resources to point to any directory you want. So you can drop the JAR file into the custom-lib folder, or create a new folder anywhere on the server filesystem.
    • Restarting Mirth Connect is not required. Instead, in the Administrator navigate to the Settings -> Resources tab. 

Image Added

    • Create a new Directory resource if needed. If you're just using the custom-lib folder, and the default resource is pointing to custom-lib (it will by default), then you only need to hit Reload Resource and the JAR will automatically be picked up. Hit the refresh button if the JAR doesn't show up initially. You should now see it in the Loaded Libraries table at the bottom.

Image Added

    • Edit the channel you want to use the resource with, go to the Summary tab, and click the Set Libraries button. Make sure the resource is checked for the context you want to use it in. If you're using the default resource, it should already be checked by default.

Image Added

    • Redeploy the channel if you made any changes to it.


Now that you've included the JAR, send a message through the channel. You should see something like this in the server log:

Code Block
[2015-05-12 10:45:54,666]  INFO  (js-connector:?): Hello, World!