Call Haxe Libraries

You can declare existant haxe classes using the @JTranscNativeClass annotation and marking methods as native.

An example could be:

private static class HaxeAdler32 {
	public HaxeAdler32() { }
	native public boolean equals(HaxeAdler32 that);
	native public int get();
	native public void update(byte[] b, int pos, int len);
	native static public int make(byte[] b);
	native static public HaxeAdler32 read(InputStream i);


static private class HaxeStringTools {
	native static String htmlEscape(String s);
	native static String htmlEscape(String s, boolean quotes);

Take into account that some types are converted when called this way.

  • Primitive types: integral types will be converted to Int, but long that will be converted to haxe.Int64, and float and double will be converted to haxe Float.
  • byte[] will be converted into
  • java.lang.String will be converted into haxe.String.
  • will be converted into When returning, it will do the opposite.
  • Other classes marked with @JTranscNativeClass will work directly without conversions.
  • There is @HaxeNativeConversion to specify a custom conversions.

This approach just works when targeting Haxe, so plase before using it, ensure you are using JTransc. Calling JTranscSystem.isJTransc() for example or with different mains injecting dependencies.

You can include haxelib libraries with the @HaxeAddLibraries annotation, and use MiniTemplates to reference java methods from Haxe code.

class WebSocketHaxe(url: String, subprotocols: Array<String>?) : WebSocket(url) {
	init {
		ws_init(url, subprotocols)

		if (p1 != null) { =, cast p1.toArray()); // Array<String>
		} else { =;
		} = function() { this{% IMETHOD %}(); }; = function() { this{% IMETHOD %}(); }; = function(m:String) { this{% IMETHOD %}(N.str(m)); };
	private fun ws_init(url: String, subprotocols: Array<String>?) {

	private fun process_int(): Unit {

	private fun process(): Unit {
		EventLoop.setTimeout(20) { process() }

	private fun onConnectSend() = onConnect.dispatch(Unit)

	private fun onDisconnectedSend() = onDisconnected.dispatch(Unit)

	private fun onStringMessageSend(msg: String) = onStringMessage.dispatch(msg)

	private fun onBinaryMessageSend(msg: ByteArray) = onBinaryMessage.dispatch(msg)

	override fun connect() = super.connect()

	override fun send(message: String) = super.send(message)

	override fun send(message: ByteArray) = super.send(message)

Edit this page (cookbook/haxe-call-libraries/