Interfacing with Java Functions in Haxe

邮差的信 提交于 2019-12-12 13:48:10

问题


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?


回答1:


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




回答2:


The reason it fails is explained here

https://groups.google.com/forum/#!topic/haxelang/EHeoGN_Ppvg

I tried setting up with class paths and various options but did not get a solution, I think it's just a bit fiddly to do externs on the java target by hand. Really it's better to use Java compiler to create jars and let haxe auto generate the externs unless you get an issue then report it to hxJava repository.




回答3:


Use -java-lib.

# build.sh
haxe Main.hx -main Main -java-lib javalib/ -java out

,

// ./Main.hx
import external.*;

class Main {
    public static function main() {
        trace(external.ExternalClass.myFunction());
    }
}

,

// ./javalib/external/ExternalClass.java
package external;

public class ExternalClass {

    public static String myFunction() {
        return "External Java function";
    }

}

,

./javalib/external/ExternalClass.class is the output of javac ExternalClass.java



来源:https://stackoverflow.com/questions/35602961/interfacing-with-java-functions-in-haxe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!