Simple java class call example

Hej,

I’m sorry as I wrote in another post, I come back into openFL for many years and I’m a bit lost about how things have changed so I permit to ask maybe a basic question.

In the past I used to deal with JNI in order to include java classes into an NME project.
I’ve taken look into some posts about JNI and it seems to have changed.

So, my concrete question is :
Let’s say I’ve a Java class that return me a result using a static class, something like that :

public class JavaClass{
    public static String getString () {
        return "Take this one";
    }
}

This class lays near my .hx files in Source directory.
How do I make a call from .hx file to this java getString function please, soimething like that ?

import lime.app.Application;
import lime.graphics.RenderContext;

class Main extends Application {
	public function new () {
		super();
		trace( MyRemoteJavaClass.getString() );
	}
}

As this question is very basic, not related to any external libs in a big project, I think it can help people who looks for something like that in the futur.

First I think I have to tell the .xml project file where to find this class and then make a “link” from this class to the .hx file in order to be able to access it from my .hx file…

Many thnaks for your advices !

var javaCall = lime.system.JNI.createStaticMethod("JavaClass", "getString", "()V");
javaCall();

It?

Thanks Rainy for your answer.

Ok so it’s quite the same as before !

How should I tell the project.xml file where to find the class please (and kind of embed it) ?

And by the way, if I have to include an external java lib, how can I do please ?

<dependency name="libname" path="dependencies/libname" />

Through this, you can import the library into the deps directory.

package;

import flixel.tile.DrawData;
import open.fl.geom;
import open.fl.utils as OpenFlUtils;
import lime.app.Application;
import lime.graphics.RenderContext;
import flixel.graphics.frames.FlxAtlasFrames;

using StringTools;
using ExprTools;

class JavaClass extends Main implements Application
{
var java:DrawData;
DrawData _format = (‘java’);
anitialising = false;

var bullShit:String = new.bullShit;

pull bullShit:Array = [
	bullShit.1 = ['java'];
	bullShit.1 = ['python'];
	bullShit.1 = ['typescript'];
	
	?java more.Languages = false;
	anitialising = false;
];
public function new()
{
	super.create;
	use.atlasFrames = false;
	new.java = new('java');
	DrawData('java') in.java = true;
	anitialising = true;
}

 var create.drearyEffect:Array<Dynamic> = (?java atlasFrames);
 ?java Boolean = false(null.Boolean = new.Boolean);
 new.Boolean = new.Bool(('true' = true), ('false = false'));

?java = new.java(false.to.true = false.to.true);
?java DrawData java = true;
anitialising = false;

}

Hej Rainy,

I have another question maybe you could also give me the answer ?
How do I call a haxe/openFL class’ method from a Java class please ?

Let’s say I have a method setString( s : String ) on haxe side in com.my.pack.MyClass

Thanks in advance,

Generally, you need to pass the haxeobject object to Java through JNI:

	private static var init = JNI.createStaticMethod("org.haxe.extension.JavaClass", "init", "(Lorg/haxe/lime/HaxeObject;)V");

Java:

public static HaxeObject _callBackObject;
public static void init(HaxeObject func)
{
	_callBackObject = func;
}

Then you need to define a haxeobject passed in the past:

class MyClass {
	
	public function new(){

	}

	public function setString(str:String):Void{
		trace("java calling!",str);
	}

}

Put to java:

var obj = new MyClass();
init(obj);

Java back to haxe:

Extension.mainActivity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if(_callBackObject != null) _callBackObject.call("setString", new Object[]{"event1"});
			}
		});
1 Like

Thanks Rainy,

That was exactly what I was looking for !

Cheers !