Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfacing with Java Functions in Haxe

Tags:

java

haxe

I am trying to call an external Java function from Haxe using "extern".

Haxe Code :

extern class Ext
{
   public static function test():String;
}

class Sample 
{
   public static function main()
   {
       trace(Ext.test());
   }
}

Java Code :

public class Ext
{   
    public static String test()
    {
        return "Hello";
    }
}

Both Sample.hx and Ext.java files are in the same folder.

When I try to execute haxe -main Sample -java Sample, I get the following error.

C:\Users\ila5\Desktop\CPP>haxe -main Sample -java Sample
haxelib run hxjava hxjava_build.txt --haxe-version 3201 --feature-level 1
javac.exe "-sourcepath" "src" "-d" "obj" "-g:none" "@cmd"
src\haxe\root\Sample.java:33: error: cannot find symbol
                haxe.Log.trace.__hx_invoke2_o(0.0, haxe.root.Ext.test(), 0.0, new haxe.lang.DynamicObject(new java.lang.String[]{"className", "fileName", "methodName"}, new java.lang.Object[]{"Sample", "Sample.hx", "main"}, new java.lang.String[]{"lineNumber"}, new double[]{((double) (((double) (10) )) )}));
                                                            ^
  symbol:   class Ext
  location: package haxe.root
1 error
Compilation error
Native compilation failed
Error: Build failed

I would like to understand why the build failed. Any ideas?

like image 752
Ranganatha Rao Avatar asked Dec 21 '25 04:12

Ranganatha Rao


1 Answers

I am not sure you might need to reference your Java code with -lib or something else?

But generally with Java target it's much simpler to just use a jar file. By typing haxe --help you will see the relevant command listed, I have never had a need to hand write externs for the Java target.

-java-lib <file> : add an external JAR or class directory library

like image 77
Justinfront Avatar answered Dec 23 '25 16:12

Justinfront