Calling Julia from within Java: JuliaCaller

Hi folks,

Due to the discussion in Running Julia from Java - What is crazier?, I am introducing a new project for calling Julia from within Java. Despite it is in its early stages, the package implements basic external calling of Julia from Java and a skeleton of JSR 223 (Java scripting interface) implementation is ready for future development.

The package simply starts an external Julia process, injects a script that listens TCP connections (multiple ones if needed) on a given port, accepts the statements and expressions and returns the results in JSON format. The JSON is then parsed to Java objects in Java side. The performans overhead consists of stream connections and JSON parsing (both encoding and decoding).

The project JuliaCaller is hosted on GitHub repo with Apache license.

Any contributions are welcome.

All the best :slight_smile:

9 Likes

Hi,
I’ve been digging into JuliaCaller a bit as I am comparing the various Julia-from-Java solutions out there and had a quick question: what made you go for tcp/json as opposed to “basic” process to process communication? (ie. via Java streams, as done in JaJuB. Did you notice any benefit (in terms of coding, performance, etc.)? I’d see a benefit of the network layer if your Julia instances were indeed cloud based but they seem local as of now (due to the code injection?). But the json format sounds like a pain when having to transport several gigs of data between languages as plain text. Thoughts?

Edit: json does sound very appealing to handle dicts out of the box though.

Hi, thank you for your interest!

This is a fast ready to go project for calling Julia from within Java and I didn’t think it would be used in a project big enough to move gigabytes of data. I wanted to try what I did in the RCaller project which is library for calling R from within Java using the same logic.

Fair enough, makes total sense.
I’ll continue fiddling with the various options on my use cases and will report my findings fwiw.

1 Like

For a command-line interface, see
A Java Matlab/Julia Interface - Stack Overflow

Hi, Is there anyway to execute Julia scripts in java using this, Can you provide some examples if possible.
Thanks
Sai Charan

1 Like

There are lots of minimum test cases like

@Test
    public void requiringPackageTest() throws IOException {
        caller.Execute("using Statistics");
        caller.Execute("d = [1,2,3]");
        caller.Execute("ave = mean(d)");
        String s = caller.GetAsJSONString("ave");
        assertEquals("{\"ave\":2.0}", s);
    }

in test folder of repository. Have you looked at there?

Hi ,
Thanks for the reply
I mean I have a multiple .jl files that needs to be executed using java

How can I achieve that ?

Maybe you can include the source file using

caller.Execute("include(\"filename.jl\")");

and then grep the results

caller.getXXX(varname);
1 Like

and here is the minimal example of doing some stuff:

caller = new JuliaCaller("/usr/local/bin/julia", 8000);
caller.startServer();
caller.Connect();

caller.Execute("mypi = 3.14159265");
JSONObject obj = caller.GetAsJSONObject("mypi");
// Assertation is true
assertEquals(obj.getDouble("mypi"), 3.14159265);

// Creating object 'myarray' in Julia
caller.Execute("myarray = [1,2,3,4,5]");

// Getting the array back in Java
JSONArray obj = caller.GetAsJSONArray("myarray");

// Assertations are true
assertEquals(obj.length(), 5);
assertEquals(obj.getInt(0), 1);
assertEquals(obj.getInt(1), 2);
assertEquals(obj.getInt(2), 3);
assertEquals(obj.getInt(3), 4);
assertEquals(obj.getInt(4), 5);

caller.ShutdownServer();

As I mentioned in previous entry, included jl files are automatically run and variables can be imported from within Java:

caller.Execute("include(\"filename.jl\")");
1 Like

Addition to this, @mkitti summarized the other options in thread Embedding Julia in the Java Virtual Machine.

2 Likes

Hi
Thanks for the reply, I will try those and get back to you. I need one more thing, Can I use juliacaller as maven dependency like this

<dependency>
        <groupId>org-expr</groupId>
        <artifactId>juliacaller</artifactId>
        <version>0.2.1-SNAPSHOT</version>
    </dependency>

I have tried this, the jars are not getting downloaded. Do I need to do any changes ?

JuliaCaller is not registered in Maven repositories and you can manually build the source code using

mvn install

Do not forget to install JSON package in Julia before using JuliaCaller.

1 Like

If you encountered problems, you can download the POM and compiled JAR file using the release Precompiled JAR file.

1 Like

Hi,
I am able to run using juliacaller in my local system tried couple of examples, working fine.
But when i tried to run the .jl file which is in my local system like this,

 caller.Execute("include(\"C:\\WorkFolder\\bin_optim_juMP.jl\")");

I am getting error as,

 Error: Base.Meta.ParseError("invalid escape sequence")
â”” @ Main none:11

Can you please help

Note: I resolved it by placing the script files directly in project path so the slashes are not needed. But if I want to give path how can i give ?

did you try typing like

path/to/file.jl

?

No I havent tried will try that