Latest blog entries

Introducing Dayron - The Elixir REST client you've always wanted

Meet our library to interact with RESTful APIs in your Elixir Applications.

May 24 2016 : Flavio Granero

Inaka's First Tech Day

Inaka's First Tech Day

May 20 2016 : Brujo Benavides


The best image projector ever. Period.

May 18 2016 : The Gera

Meet Jayme

Your best friend when it comes to abstract server interconnections in Swift

May 09 2016 : Pablo Villar

What's in a Hackathon?

The benefits of Hackathons, and a summary of ours

May 01 2016 : Stephanie Goldner

How do we host our internal tools ...

Inaka internal hosting

Apr 14 2016 : Ignacio Mendizabal

Last Year Meetups at Inaka

Last Year Meetups at Inaka

Mar 01 2016 : David Cao

5 Critical Lessons of App Building

While most may think that the meat of building an app is in the development, we’ve found that a large part of a successful project is the work done up front.

Feb 29 2016 : Stephanie Goldner

Presenting Lewis, our own Android Lint extension

Rock your Android

Feb 15 2016 : Fernando Ramirez

KillerTask, the solution to AsyncTask implementation

Meet our new Android library written in Kotlin

Jan 25 2016 : Fernando Ramirez

Android development with Kotlin

How to set up and use Kotlin language in Android apps

Jan 15 2016 : Fernando Ramirez

Canillita (Your First Erlang Server) - V2

Learn Erlang by example creating a simple RESTful server

Jan 04 2016 : Harenson Henao

Dealing with Optionals in ViewControllers

The right way to declare your variables

Dec 18 2015 : Pablo Villar

Testing Distributed Apps with Common Test

Testing Distributed Apps with Common Test

Dec 11 2015 : Carlos Andres Bolaños

Erlang Serpents

We've built a serpents game in Erlang and we played with it

Nov 13 2015 : Brujo Benavides

Erlang Meta Testing Revisited

Meta-Testing in Erlang. Now it's much easier.

Nov 13 2015 : Brujo Benavides

Here’s to 5 years

It's our 5th birthday, let's celebrate!

Sep 01 2015 : The Inako

Open-sourcing at Inaka (lasse, egithub, tirerl & more)

Contribute to open-source and learn at the same time.

Aug 25 2015 : Juan Facorro

Cowboy Swagger

Swagger integration for Cowboy

Aug 19 2015 : Carlos Andres Bolaños

Too big to compile

What's the maximum size of a source file in Erlang?

Aug 05 2015 : Hernán Rivas Acosta

See all Inaka's blog posts >>

From Erlang to Java and Back Again: Part 1

Fernando "Brujo" Benavides wrote this on September 05, 2013 under dev, erlang, java .


Not so long ago, I was required to write an Erlang application that uses some Java libraries through JInterface. I finally did it, and I learned a lot of things in the process. This article is the first one in a series describing the things I've learned along the way. As we walk through this series, we'll create an application like the one I wrote. It will be called fetjaba (as in From Erlang To Java And Back Again).

In This Article

One of my first goals was for this new application to be used as any other OTP Application, not requiring any Java knowledge from its users. For that, I needed to be able to:

  • compile its Java pieces along with the Erlang ones using rebar compile and, in general, be able to include the app as a regular rebar dependency
  • start the application with just application:start/1 on an Erlang shell, without any Java or shell commands to start the Java part
  • stop the application with application:stop/1 on an Erlang shell, not leaving any Java process(es) around when the application stops

In this article, I'll show you how I implemented that based on this library written by @fredlund.

Directory Structure

For this project I used the standard OTP directory structure, including the classical ebin, src and include folders, deps for the rebar dependencies (like lager), etc. I put the java code in a new java_src folder (in the root folder of the project) and I put all the external jars I needed in the priv folder. I also created a bin folder to place the compiled java classes.

Compiling Java Code

As I stated before, I wanted to be able to just rebar compile the project and have all the erlang and java code compiled at once. To do this, I added the following lines to my rebar.config:

{pre_hooks, [{compile, "mkdir -p bin"}]}.
{post_hooks, [{clean, "rm -rf bin priv/fejtaba.jar"},
              {compile, "javac -g -verbose -deprecation -sourcepath java_src 
                               -classpath ./bin:/usr/local/lib/erlang/lib/jinterface-1.5.6/priv/OtpErlang.jar:./priv/*
                               -d bin `find java_src -name *.java`"},
              {compile, "jar cf priv/fejtaba.jar -C bin ."}]}.

The pre_hook for compile basically just creates a folder where java will output the .class files. Then, I added 3 post_hooks: one for cleaning up by removing the jar generated when compiling, one for compiling the java sources into the bin folder, and the last one to put them all into a jar file in priv.

Starting the Java Node from Erlang

The next task was to be able to start the java node when the Erlang application starts, as well as detect if it goes down at some point. To do so, I wrote the following code:


On the Erlang side, I created a gen_server with the following init function:

init([]) ->
  %% First of all we need to have java installed
  case os:find_executable("java") of
    [] ->
      throw({stop, java_missing});
    Java ->
      %% We want to let the java node know who we are
      ThisNode = atom_to_list(node()), 
      %% We build a fancy name for the java node
      JavaNode =
        case string:tokens(ThisNode, "@") of
          [Name, Server] -> list_to_atom(Name ++ "_java@" ++ Server);
          _Node -> throw({bad_node_name, node()})
      %% Finding the priv dir is a tricky thing, this function does it for us
      Priv = priv_dir(fetjaba),
      %% In the same way, finding an otp lib dir is also tricky, hence otp_lib
      %% We build the classpath with our priv dir (where we put all the jars we need)
      %% and OtpErlang.jar from jinterface
      Classpath = otp_lib("/OtpErlang.jar") ++ [$: | Priv ++ "/*"],
      Port =
        erlang:open_port({spawn_executable, Java},
                         [{line,1000}, stderr_to_stdout,
                          {args, ["-classpath", Classpath, %% The classpath
                                 %% The class with the main method on our Java code
                                  %% The command line arguments
                                  %% including the node names and
                                  %% the cookie (you'll see how I use them on java side)
                                  ThisNode, JavaNode,
      %% Then we wait for a signal from the Java side that everything is ready
      wait_for_ready(#state{java_port = Port, java_node = JavaNode})

%% @doc Skips every console message until it finds the word "READY"
%%             Then starts monitoring the Java node
wait_for_ready(State = #state{java_port = Port}) ->
    {Port, {data, {eol, "READY"}}} ->
      true = erlang:monitor_node(State#state.java_node, true),
      {ok, State};
    Info ->
      case handle_info(Info, State) of
        {noreply, NewState} ->
        {stop, Reason, _NewState} ->
          {stop, Reason}

Since we are monitoring the Java node, we can handle 'nodedown' messages, and at least stop this gen_server, so its supervisor can act accordingly:

handle_info({nodedown, JavaNode}, State = #state{java_node = JavaNode}) ->
  lager:error("Java node is down!"),
  {stop, nodedown, State};


On Java side, I created a class called net.inaka.fetjaba.Node that, up to this point, just starts a node:

public class Node {
  /** This Erlang node */
  public static OtpNode   NODE;

  /** Peer node name */
  public static String    PEER;

  public static void main(String[] args) {
    String peerName = args.length >= 1 ? args[0]
        : "fetjaba@nohost";
    String nodeName = args.length >= 2 ? args[1]
        : "fetjaba_java@nohost";
    try {
      // Depending on whether there is a provided cookie or not…
      NODE = args.length >= 3 ? new OtpNode(nodeName, args[2])
          : new OtpNode(nodeName);
      PEER = peerName;

      do_our_stuff(); // Most likely, at least start a thread with an Erlang mbox

    } catch (IOException e1) {

Stopping the Java Node from Erlang

My last goal was to be able to stop the running Java node when I executed application:stop(fetjaba) on the Erlang side. There are many ways to do it, but since I already had a gen_server on the Erlang side, I decided to put an Erlang Mbox (which is almost equivalent to an Erlang process) on Java side and just link it to my gen_server. This way, when one dies, the other can take action. So, at some point inside what replaced that call to do_our_stuff() on the Java code, I started a Thread with a named Mbox and handled the exit signals like this:

    // We create an mbox to link
    final OtpMbox mbox = NODE.createMbox("fetjaba_server");
    new Thread(mbox.getName()) {
      public void run() {
        while(true) { // This thread runs forever
          try {
            OtpErlangObject msg = mbox.receive(); // Gets the message
            processMsg(msg); // … and deals with it, before getting the next one
          } catch (OtpErlangExit oee) { // Until it gets an exit signal
            System.exit(1); // And, this is a little bit violent, but it gets the job done.

Then, on the Erlang side, we change our wait_for_ready/1 function just a little bit…

wait_for_ready(State = #state{java_port = Port}) ->
    {Port, {data, {eol, "READY"}}} ->
      true = erlang:link(process()),
      true = erlang:monitor_node(State#state.java_node, true),
      {ok, State};
    Info ->
      case handle_info(Info, State) of
        {noreply, NewState} ->
        {stop, Reason, _NewState} ->
          {stop, Reason}

I coded the processMsg(msg) method on my Java class to handle stop and {pid, pid()} properly so that we could see the application in action:

protected static boolean processMsg(OtpErlangObject msg, OtpMbox mbox)
      throws OtpErlangDecodeException {
    if (msg instanceof OtpErlangAtom
        && ((OtpErlangAtom) msg).atomValue().equals("stop")) {
      return false;
    } else if (msg instanceof OtpErlangTuple) {
      OtpErlangObject[] elements = ((OtpErlangTuple) msg).elements();
      if (elements.length == 2) {
        if (elements[0] instanceof OtpErlangAtom
            && ((OtpErlangAtom) elements[0]).atomValue().equals(
                "pid") && elements[1] instanceof OtpErlangPid) {
          OtpErlangPid caller = (OtpErlangPid) elements[1];
          mbox.send(caller, new OtpErlangTuple(new OtpErlangObject[] {
              elements[0], mbox.self() }));
          return true;
    throw new OtpErlangDecodeException("Bad message: " + msg.toString());

Trying Things Out For Real

Now let's try all this stuff out, shall we? Since, at this point, it's just a rebarized OTP Application, we can start it inside an Erlang node, like this:

1> application:start(fetjaba).

=INFO REPORT==== 17-Jul-2013::16:57:18 ===
Java node started


=PROGRESS REPORT==== 17-Jul-2013::16:57:18 ===
         application: fetjaba
          started_at: 'fetjaba@inaki.local'

We can then check that the java node is up, and responds to our messages:

2> net_adm:names().
3> fetjaba:process().

Now we should verify that both sides of the app die at once. Let's start by tearing down the Java node first:

4> fetjaba:stop().
=ERROR REPORT==== 17-Jul-2013::17:14:06 ===
Java node is down!

=ERROR REPORT==== 17-Jul-2013::17:14:06 ===
** Generic server fetjaba terminating

=INFO REPORT==== 17-Jul-2013::17:14:06 ===
    application: fetjaba
    exited: nodedown
    type: temporary

5> net_adm:names().
6> erlang:whereis(fetjaba).

So, when the Java process terminated, its node was torn down. Then, our gen_server noticed it and stopped our application. That's what we wanted. Now, what happens if we stop our application instead?

7> application:start(fetjaba).

=INFO REPORT==== 17-Jul-2013::17:20:27 ===
Java node started


=PROGRESS REPORT==== 17-Jul-2013::17:20:27 ===
         application: fetjaba
          started_at: 'fetjaba@inaki.local'
8> net_adm:names().
9> fetjaba:process().
10> application:stop(fetjaba).

=INFO REPORT==== 17-Jul-2013::17:21:42 ===
    application: fetjaba
    exited: stopped
    type: temporary
11> net_adm:names().
12> fetjaba:process().
** exception throw: timeout

As you can see, when we stop our application, the java process is stopped, too. Exactly what we wanted!

See For Yourself

The code for this application is hosted on Inaka's Github. You can clone that repo and try all this stuff out. We also accept pull requests! :)

What's next?

In the following articles on this series, I'll show you how to create and start some useful processes on the Java node, as well as how to communicate back and forth between them and the Erlang ones.