Haxe microservice with Java target


(hjeff) #1

I’d like to write a simple java microservice using Haxe and java target.
What would be the path of least resistance to do this ? I could not find any resource on how to glue it together with something like Javalin or Spark.

(hjeff) #2

bump please

(david mouton) #3

Do you have a simple java exemple ?

(Dmitry Hryppa) #4

I’m not familiar with Javalin (or Spark). But I’ll try to help you.

I must say that Haxe has -java-lib flag for injecting jar libraries into the project and auto generating externs, but unfortunately it is not support Java8+ libs, so you need to build your sources manually and write externs (bindings) by hands if you want to use modern libraries.

Okay, let’s try to run Javalin. Javalin is a modern lib, so we will make everything manually.

Create src folder and put this Main.hx file into it:


class Main implements Handler {
    public static function main():Void new Main();

    public function new() {
        var app:Javalin = Javalin.create().start(7000);
        app.get("/", this);

    public function handle(ctx:Context):Void {
        ctx.result("Hello world!");

//Javalin externs
extern class Javalin {
    public static function create():Javalin;
    public function start(port:Int):Javalin;
    public function get(path:String, handler:Handler):Void;

extern interface Handler {
    public function handle(ctx:Context):Void;

extern class Context {
    public function result(value:String):Void;

Now, create build.hxml file in the root of your project folder with this content:

-lib hxjava
-cp src
-main Main
-java ./out

-dce full

#We don't want to compile it with Haxe. Wil will do it with gradle manuallty:
-D no-compilation

Next, we need to use Gradle (I’m using 4.10.2 ver) to compile our sources. So, create build.gradle file in the root with this content:

plugins {
    id 'java'
    id 'application'

sourceCompatibility = 1.8

repositories {

sourceSets { += './out/src'

dependencies {
    compile group: 'io.javalin', name: 'javalin', version: '2.3.0'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'

mainClassName = 'haxe.root.Main'

Okay, now you should have this hierarchy in your project folder:


Now, you need to run gradle wrapper command to initialize Gradle wrapper in your project folder.

Next, let’s generate Java sources with Haxe, call haxe build.hxml
Next, run ./gradlew build to build previously generated Java sources.
Next run ./gradle run to run your Javalin server.

Done :slight_smile: Open http://localhost:7000 in your browser and you shold see "Hello World" string

P.S. If you want to automate Haxe building process with Gradle, then you can add something like this in the end of build.hxml:
-cmd ./gradlew build
So every time you run Haxe build.hxml it will run Java building process too.

Or you can put Haxe task into Gradle task.
Or you can automate everything with VSCode tasks.
Choose what you prefer more.

Well, hope it helps you to start.

(hjeff) #5

Thank you for the tutorial. Much appreciated.
I am not sure anymore if I want to go with javalin If i have to manually translate everything :confused:

(Dmitry Hryppa) #6

Well, you need to setup everything only once. And all build process can be automated into one task.

(hjeff) #7

I thought I would still need to translate the api to externs.

(Dmitry Hryppa) #8

That’s true. I created issue about this.