Getting started with Haxe for Java


Install Haxe

1. Download and install the Haxe Compiler.
2. Run this command in your console to globally install Java support in Haxe:
haxelib install hxjava

Install a Haxe-compatible IDE

It is recommended to install one of these to develop your Haxe projects.

Create your project

1. Create a new directory for your Haxe project, with this structure:
├── src/
│   └── Main.hx
├── bin/
│   └── java/
└── build.hxml
2. Add the following code into the src/Main.hx file:
class Main {
  static function main() {
    Sys.println("Haxe is great!");
3. Add the following build configuration into the build.hxml file:
-cp src
-main Main
-java bin/java
The configuration above specifies that your source code is stored in src/, that your main Haxe file is src/Main.hx, and that you want Haxe to output the Java source code into bin/java/. After code generation, Haxe will automatically detect the installed Java compiler and use it to compile your project into a JAR file in bin/java/.

Install the Java runtime and SDK

Haxe requires Java 6 or later. Due to security fixes and new features Java 8 or later is recommended.

1. Install the latest Java Runtime Environment (JRE).
2. Test your installation by opening a command prompt and typing java -showversion.
  • If your console cannot find java, you have an issue with your installation or the JRE is not added into the PATH environment variable.
  • If the installation was successful, you should see something like the following:
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
3. (Optional in Haxe 4) Install the Java Software Development Kit (SDK).
  • The Java SDK is required for development in Haxe 3, since it can only output Java source code, not the final .jar file.
  • The Java SDK includes the javac compiler that converts .java sources into a .jar file.
  • Since Haxe 4, the Java source code step can be skipped entirely by using the -D jvm define to output .jar files directly.
4. (Optional) If you are intending to develop mobile applications for Android, install the SDK of the target Android version.

Develop your project

Haxe API

Browse the Haxe API website for the core Haxe APIs that you can use in your project.
You can use Java-specific APIs in addition to the core Haxe APIs.

Haxe libraries

Browse the haxelib website for community-developed libraries that you can add to your project.
You can install haxelib libraries globally by running haxelib install <library>.
You can specify that your project uses a haxelib by adding -lib <library> to your .hxml file.


Compile and run your project


Build your Haxe project by running haxe build.hxml in a console in your project directory. This compiles your .hx source code into a Java program which you can run and debug next.

Running and debugging

After compiling your project with Haxe and Java, you'll need to open a console in the bin/java directory and run the command java -jar Main.jar
You can alternatively use a full-fledged Java IDE like the ones given below to run and debug your project easily.

Install a Java IDE

If you want a better debugging experience, you can install a dedicated Java IDE to run and debug your Java programs generated from your Haxe project.

Ask the community

If you need help with anything, visit the vibrant Haxe community and simply ask for help with the details of your project. We have a helpful and active community and you should get your answers quickly!