Unity and Java marriage

Image credit: Artur Pachałko

Unity and Java marriage

Emilia Szymańska bio photo By Emilia Szymańska Comment

Project repo still is empty as my fridge during early years of studies, but there is a reason for that. I cannot make a general design decision without figuring out one thing – how to use these wonderful temporal logic system libraries written in Scala from the Unity’s C# script? Is it even possible? If it wasn’t possible, I would have to create for myself some sort of graph database, where I would store history graphs generated by the temporal logic system. That is some kind of a workaround, but it wouldn’t let me benefit from all of the perks of temporal logic. Ultra-fast googling told me – it is possible!

Importing jar libraries into Unity

I found a perfectly suitable question on Unity Answers. Obviously the only one real life use case for such a weird thing is the situation, when you develop an Android app and need to use Android-specific plugins. Flying on the wings of documentation I did not think really deeply what that could mean, I just simply created a test script. Importing classes and object require using such classes as for example AndroidJavaObject. Then I saw something that made my heart stop - compilation error. Of course that trick works only when the app is run on an Android device. Otherwise it wouldn’t compile and therefore I couldn’t see the result in editor’s console. My bad.

Java -> .Net?

Then I had this crazy idea – what if I could somehow transform my ugly .jar file into the beautiful .dll library? My great friend google told me that of course there already is the project that can perform this task. I found IKMV.NET.

As we can read on the Home page of this project - IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:

• A Java Virtual Machine implemented in .NET,

• A .NET implementation of the Java class libraries,

• Tools that enable Java and .NET interoperability.

We also get a tool called ikvmc, which is a Java bytecode to .Net IL translator. That really sounds magic and exciting, so I happily downloaded the package from the Download page of the project website, added the bin folder to my PATH variable and tried compiling my test .jar. Tum tum tum…


Did I really expect it to work out of the box, even after reading this blog post? Yes I did. After some research it turned out that my test jar probably uses Java 8, which is not supported by that version of IKVMC. My heart stopped again but only for a while – I found the RC version that does support it. Then I accomplished my first success:


Looks like it worked…!


Tum tum tum, again. Console revealed the true root of my problem – missing library.


I dropped this file near my new exe file, still got an error. Lost my patience, dropped all dll files connected to ikvmc. THEN IT WORKED.


The only thing left was to create a dll file instead of an exe, which required just a slightly modified command:


Importing a dll into the Unity project is really simple. I created an empty game object on my pure new Game Scene and added some silly, testy script:

using UnityEngine;
using com.company;

public class JarTest : MonoBehaviour {
	void Start () {
        var test = new HelloWorld();

I wouldn’t be me if I hadn’t forgotten to drop all this ikvmc dll files near my test dll in the Unity Plugins directory….


THEN I HAD ANOTHER ERROR. I even forgot to take a screenshot. There was some kind of “ReflectionTypeLoadException”. That was just pure Unity’s nastiness, because to fix that error I had to set “Api Compatibility Level” to “.NET 2.0” instead of “.NET 2.0 Subset” (???) in the Player settings. I saved the project, but the error was still visible in the console. I returned to my browser in desperate search for another solution, then I maximized Unity window to check something out… And the console was clear. Pure magic, something must have taken some time to reload. And this is the end of our adventure, since I saw this output:



I am really in awe for the ikvm project, guys did a great job in marrying java to C#. However, this minor success does not mean that a jar file containing some code compiled from Scala language classes will necessarily work. We’ll see how it goes.

Off – topic

I was thinking really hard whether to write posts in English or not. I caught myself on planning the texts in my head already in that language… Finally I decided to write this post in Polish, just to ask you, my dear reader, which one you prefer more. Then I realized that… The font I’m using doesn’t support polish letters. :)

comments powered by Disqus