Getting started with Haxe/Java

Starting from Haxe 2.10, the Haxe compiler can generate Java source code from your Haxe code (in beta).

Java Quickstart

Prerequisites

  • Let's first download and install the Haxe compiler.
  • In order to be able to compile the generated Haxe/Java code, you will need to download the JDK (Java Development Kit, includes the javac compiler). You can also go with an open-source alternative, with OpenJDK.

All done? Now let's try some code!

My first Haxe/Java program


Let's start with a "Hello World" code!
class Main 
{
    public static function main() 
    {
        trace("Hello world");
    }
}
  • Create a new folder and add a file named Main.hx (with the script from above).
  • In the same folder, create a file named build.hxml with the following content:
-main Main
-java java
  • Double click the build.hxml file or run it from commandline:
    haxe build.hxml

    The haxe compiler will output the generated Java source code to the java folder.

Troubleshooting

I'm getting Error: Library hxjava is not installed : run 'haxelib install hxjava'


In order to automatically compile the generated java code, the compiler needs an extra haxelib called hxjava. To download it, open the command-line terminal (If you're in Windows, press Win+R and type cmd; If you're on the Mac or Linux, open the Terminal), and type:
haxelib install hxjava

That's it!

If you do not wish to automatically compile the code, add the -D no-compilation compiler option to your build.hxml file

Java compiler not found


If you see this message when compiling, the hxjava lib can't find your JDK folder. Please first make sure if you have installed it (see Prerequisites above). If you have, you may need to define some environment variables to help hxjava find it:
You can either put the installed path into your JAVA_HOME environment variable, or add its bin folder to your PATH.

Running your application

  • navigate to the 'java' folder
    cd java
  • and run the application:
    java -jar java.jar

You should see:

Main.hx:5: Hello world

All done? Continue with the next Haxe/Java tutorial section:
>> Interfacing with native Java code: Creating externs

Notes


Issues


The java target is still under development, don't hesitate to report issues here.
version #19656, modified 2013-08-20 17:46:51 by shohei909