Can I access Java code from Unity?

I have an existing computational library that does high level AI reasoning in Java. Can I connect my Java library for use in a Unity game? If so, what is the best way to do so?

I have read the documentation on plugins (http://unity3d.com/support/documentation/Manual/Plugins.html). It is very clear that this can be used to connect to external code in c++. However, the phrase "Practically any language or development environment that can create DLL files can be used to create plugins" got my attention.

If I could compile my Java code into a dll, then I could use it with Unity? Has anyone tried this?

After some research I found IKVM.NET (http://www.ikvm.net/), whose stated purpose 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 " Specifically, it discusses using Java APIs in .net applications (http://www.ikvm.net/devguide/net2java.html) and indicates it can "Convert a Java API to .NET CIL using ikvmc. This produces a .NET dll"

So if I produced a dll using IKVM.NET, could I then access my Java library from C# unity code? Would I need to use the plugin procedure described in the documentation for connecting to c++ code? My understanding is that the plugin procedure is only necessary for accessing unmanaged dlls. But I believe that the dll produced by IKVM.NET would be a managed dll, so it should be able to be placed directly in my assets directory? Any thoughts on if this is correct and/or possible?

I am very interested in hearing from anyone with advice on how to connect Unity to a Java library.

Thanks,

Angela

For those following along. The answer is yes - it works! Here is what I did:

Created a simple java library that looks like this:

public class ExampleLibrary {

private int n = 0;

public ExampleLibrary()
{
}

public String HelloWorld()
{
    String s = "Helloworld: " + n;
    n++;
    return s;
}

}

Compile it into a jar file (by any way you like, I used Eclipse).

Use ikvmc to convert the jar file to a .net .dll using the instructions here: http://sourceforge.net/apps/mediawiki/ikvm/index.php?title=Tutorial

Place my .dll, along with the ikvm dlls into my Unity project's Assets folder.

Added a GUIText object to my camera. Then added the following simple .cs script to my camera:

using UnityEngine;

public class JavaHelloWorldScript : MonoBehaviour {

ExampleLibrary exampleLibrary = new ExampleLibrary();

void Start() {

}

void OnGUI () {

string text = exampleLibrary.HelloWorld();
GUILayout.Label(text);</p>

}

void Update() { }

}

When I run, this places a string in the upper left corner of my screen that says Helloworld:X, where x is a continuously incrementing number.

So looks like I am good to go with bringing in a real Java library for use.

IF LKVM.NET is able to create a .net java VM, that is able to run your javacode, chances are very good it will run within Unity. (I have no idea how fast that java vm would be though).

You would not need to use the "plugin" system, as you are correct that that is only for unmanaged dll's, and the output of LKVM.NET would be a managed dll.

Usually with as exotic routes as these, the only way to realy find out if it works, is to try it.

Hello… I am new to unity 3d, in fact this is my first trial to this software. I want to build a virtual world using unity 3d, and also add a reasoning to the character in the virtual world. As a result of some search I found some agent reasoning programming languages (such as jade, jason, jack,…) which are build over java programming language.

Does anybody try to integrate agent reasoning language with unity 3D character? what is the best agent reasoning language to use with unity 3d?

I also tried to follow the above steps to integrate dll file, I create folder called (Plugins)and paste the dll file ExampleLibrary.dll along with 27 dll files of ikvmc , but I get error (Assets/JavaHelloWorldScript.cs(8,9): error CS0246: The type or namespace name `ExampleLibrary’ could not be found. Are you missing a using directive or an assembly reference?)

does anybody know why I get this error?

Thanks in advance