COMMUNITY

How to create a jar file from some Haxe code?

haxe-java

(John Gabriele) #1

I’d like to use some Haxe code from within an existing Java program — that is, (I think this is the customary way) I want to first compile my Haxe code into a jar file, so I can then use that jar file from my Java program. My Java is a little rusty.

To create the jar file, I’ve made a tiny hello-world Haxe library:

my-haxe-lib/
    README.md
    my_stuff/
        Foo.hx

in Foo.hx:

package my_stuff;

class Foo {
    public static function do_stuff() {
        trace("Hi from Haxe!");
    }
}

How can I create a jar file from this? I’ve installed hxjava (haxelib install hxjava), then tried:

cd my-haxe-lib
haxe -java my_stuff/Foo.hx  # nope
haxe -java my_stuff         # nope

but neither of those seem to be right.

My understanding is that once I have the jar file, I can use it just as I’d use any other jar file from my Java program.

Thanks!


#2

Your “main” function needs to be called main

And then try:

haxe -main my_stuff.Foo -java my_stuff/Foo

The “main” option is your main class. The -java option identifies your target as java with its value being the folder where the jar will appear.

I think you will find everything you need here: https://haxe.org/manual/compiler-usage.html

You can also use hxml to put all the command line flags in a file for convenience.


(Philippe) #3

If the code will be used from a Java app you should build without -main, and you will want to add annotations to types that Java will use, and make sure you expose a usable “API” to the Java side:


(John Gabriele) #4

Thanks for the link to the manual page. Missed that!


(John Gabriele) #5

Thank you, Philippe! I’m almost there! Please note, I’m doing this from the command line on a Debian GNU/Linux system with OpenJDK 10 installed:

$ java -version
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Debian-1)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Debian-1, mixed mode)

$ javac -version
javac 10.0.2

I saw the haxe command as shown in the StackOverflow post. I see that the -cp src tells haxe where to find my Haxe source code, so I changed my library directory to match:

my-haxe-lib/
    README.md
    src/
        my_stuff/
            Foo.hx

I see that the -java my_stuff option tells haxe where to put the output of compilation.

When I run the command from the my-haxe-lib directory:

haxe -cp src -java my_stuff -D no-root my_stuff.Foo

I get a lot of stuff generated in a newly-created my_stuff directory, including a my_stuff.jar. Woot!

I then copied that my_stuff.jar file into the-java-proj directory (which is just my own hello-world Java app). This directory contains a Main.java:

import my_stuff.Foo;

class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
        Foo.do_stuff();
    }
}

I can build that project via javac -cp my_stuff.jar Main.java, but when I run it (via java Main) I get errors:

$ java Main
Hello, Java!
Exception in thread "main" java.lang.NoClassDefFoundError: my_stuff/Foo
	at Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: my_stuff.Foo
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
	... 1 more

Any ideas on where I’m going wrong?

Thanks!


(John Gabriele) #6

Couple extra notes:

When I build the jar, it goes like:

$ haxe -cp src -java my_stuff -D no-root my_stuff.Foo
haxelib run hxjava hxjava_build.txt --haxe-version 3407 --feature-level 1
javac "-sourcepath" "src" "-d" "obj" "-g:none" "@cmd"

Here’s what’s in the jar:

$ jar tf my_stuff.jar 
META-INF/
META-INF/MANIFEST.MF
my_stuff/
my_stuff/Foo.class
haxe/
haxe/lang/
haxe/lang/Function.class
haxe/lang/IHxObject.class
haxe/lang/StringRefl.class
haxe/lang/Enum.class
haxe/lang/Runtime.class
haxe/lang/StringExt.class
haxe/lang/IEquatable.class
haxe/lang/DynamicObject.class
haxe/lang/EmptyObject.class
haxe/lang/Closure.class
haxe/lang/VarArgsBase.class
haxe/lang/HxObject.class
haxe/lang/Exceptions.class
haxe/lang/HaxeException.class
haxe/lang/VarArgsFunction.class
haxe/lang/FieldLookup.class
haxe/lang/ParamEnum.class
haxe/Log_Anon_47__Fun.class
haxe/Log.class
haxe/root/
haxe/root/Reflect.class
haxe/root/Std.class
haxe/root/StringBuf.class
haxe/root/Type.class
haxe/root/Array.class
_Array/
_Array/ArrayIterator.class

(Philippe) #7

Yeaaah I can confirm the same thing on my side… It’s been a while so I’m not sure.


(John Gabriele) #8

Thanks for confirming, Philippe.


(John Gabriele) #9

Ok. Got it. When running, needed:

java -cp .:my_stuff.jar Main

To help gather up this sort of platform-specific getting-started info, I started https://github.com/uvtc/haxe-getting-started-tutorials .

For these haxe-in-a-java project notes:


(Philippe) #10

Makes sense, if you only compile your .java file to .class it doesn’t include the .jar dependency.

I used to try it in an Android project so Android Studio was taking care of embedding the dependencies…