# Calling Julia from within Java: JuliaCaller

**URL:** <https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357>\
**Category:** Package Announcements\
**Created:** [October 14, 2020, 11:48am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357 "2020-10-14T11:48:49Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [October 14, 2020, 11:48am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/1 "2020-10-14T11:48:49Z")

</div>

Hi folks,

Due to the discussion in [Running Julia from Java - What is crazier?](https://discourse.julialang.org/t/running-julia-from-java-what-is-crazier/31662), 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](https://github.com/jbytecode/juliacaller) with Apache license.

Any contributions are welcome.

All the best 🙂

---

<div class="post-metadata">

**Author:** ![DrDaleks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drdaleks/32/28139_2.png) [@DrDaleks](https://discourse.julialang.org/u/DrDaleks)\
**Post date:** [August 14, 2021, 10:42pm UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/2 "2021-08-14T22:42:19Z")

</div>

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](https://github.com/org-arl/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.

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [August 16, 2021, 8:39am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/3 "2021-08-16T08:39:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DrDaleks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drdaleks/32/28139_2.png) [@DrDaleks](https://discourse.julialang.org/u/DrDaleks)\
**Post date:** [August 16, 2021, 8:42am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/4 "2021-08-16T08:42:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [August 17, 2021, 8:47pm UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/5 "2021-08-17T20:47:37Z")

</div>

For a command-line interface, see  
[A Java Matlab/Julia Interface - Stack Overflow](https://stackoverflow.com/questions/68822890/a-java-matlab-julia-interface)

---

<div class="post-metadata">

**Author:** ![charcharan](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@charcharan](https://discourse.julialang.org/u/charcharan)\
**Post date:** [December 15, 2021, 9:59am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/6 "2021-12-15T09:59:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [December 15, 2021, 10:14am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/7 "2021-12-15T10:14:17Z")

</div>

There are lots of minimum test cases like

```java
@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](https://github.com/jbytecode/juliacaller/tree/master/src/test/java/org/expr/juliacaller). Have you looked at there?

---

<div class="post-metadata">

**Author:** ![charcharan](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@charcharan](https://discourse.julialang.org/u/charcharan)\
**Post date:** [December 15, 2021, 10:17am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/8 "2021-12-15T10:17:56Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [December 15, 2021, 10:23am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/9 "2021-12-15T10:23:38Z")

</div>

Maybe you can include the source file using

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

```

and then grep the results

```java
caller.getXXX(varname);

```

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [December 15, 2021, 10:27am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/10 "2021-12-15T10:27:13Z")

</div>

and here is the minimal example of doing some stuff:

```java
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:

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

```

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [December 15, 2021, 10:29am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/11 "2021-12-15T10:29:40Z")

</div>

Addition to this, @mkitti summarized the other options in thread [Embedding Julia in the Java Virtual Machine](https://discourse.julialang.org/t/embedding-julia-in-the-java-virtual-machine/51444).

---

<div class="post-metadata">

**Author:** ![charcharan](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@charcharan](https://discourse.julialang.org/u/charcharan)\
**Post date:** [December 15, 2021, 10:36am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/12 "2021-12-15T10:36:50Z")

</div>

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

```julia
<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 ?

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [December 15, 2021, 10:40am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/13 "2021-12-15T10:40:30Z")

</div>

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

```shell
mvn install

```

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

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [December 15, 2021, 10:47am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/14 "2021-12-15T10:47:34Z")

</div>

If you encountered problems, you can download the POM and compiled JAR file using the release [Precompiled JAR file](https://github.com/jbytecode/juliacaller/releases/tag/v0.2.1).

---

<div class="post-metadata">

**Author:** ![charcharan](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@charcharan](https://discourse.julialang.org/u/charcharan)\
**Post date:** [December 15, 2021, 1:45pm UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/15 "2021-12-15T13:45:20Z")

</div>

> [@jbytecode](#):
>
> `caller.Execute("include(\"filename.jl\")");`

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,

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

```

I am getting error as,

```julia
 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 ?

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [December 15, 2021, 5:08pm UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/16 "2021-12-15T17:08:51Z")

</div>

did you try typing like

```julia
path/to/file.jl

```

?

---

<div class="post-metadata">

**Author:** ![charcharan](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@charcharan](https://discourse.julialang.org/u/charcharan)\
**Post date:** [December 16, 2021, 12:56pm UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/17 "2021-12-16T12:56:43Z")

</div>

No I havent tried will try that

---

<div class="post-metadata">

**Author:** ![colder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colder/32/216257_2.png) [@colder](https://discourse.julialang.org/u/colder)\
**Post date:** [April 11, 2025, 6:41am UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/18 "2025-04-11T06:41:01Z")

</div>

Hi, this seems to be an older thread. However, just to share additional option of calling julia from java (primitively), I use locally for some simple scenarios calling via command line. If that helps someone.

String command = “julia C:\project\julia\test.jl "nameOfTheFunction"”;  
command = command + " " + m\_textField.getText() + " " + zTextField.getText() + " " + xTextField.getText() + " " + yTextField.getText();

and then:

try {  
String returnValue = SystemCommandExample.executeSystemCommand(command);  
System.out.println("Return Value: " + returnValue);  
resultTextPane.setText(“The calculated result is " + returnValue + " cm2”);  
} catch (IOException | InterruptedException ex) {  
ex.printStackTrace();  
}

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [April 11, 2025, 8:46pm UTC](https://discourse.julialang.org/t/calling-julia-from-within-java-juliacaller/48357/19 "2025-04-11T20:46:48Z")

</div>

To be honest, I’m not even sure if Julia is currently used as a backend within Java applications. This was something I explored a while ago, inspired by how R was integrated into Java using RCaller. JuliaCaller was an attempt to replicate that approach. While you can certainly invoke a Julia instance as a separate process, making sequential calls is inefficient due to Julia’s well-documented startup latency.

While JuliaCaller aimed to facilitate sequential calls using sockets, I’m frankly not confident about its current applicability. Industry changes by time.
