Haxe microservice with Java target

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.

bump please

Hi,
Do you have a simple java exemple ?

Hi,
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:

package;

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
//--------------------------------------------
@:native('io.javalin.Javalin')
extern class Javalin {
    public static function create():Javalin;
    public function start(port:Int):Javalin;
    public function get(path:String, handler:Handler):Void;
}

@:native('io.javalin.Handler')
extern interface Handler {
    public function handle(ctx:Context):Void;
}

@:native('io.javalin.Context')
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 {
    jcenter()
    mavenCentral()
}

sourceSets {
    main.java.srcDirs += './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:

build.hxml
build.gradle
src/Main.hx

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.

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:

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

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

That’s true. I created issue about this.