# compiling haxe to java is really cool!

I recently (~7 hours ago) got the opportunity to play with the new and upcoming java target for haxe (c/o @cwaneck), and it was okay.
Okay.. I was trying to down play it, but I can’t do it. It’s really cool!

I’m going to write down exactly what I had to do to go from haxe to java, from java to bytecode, and from bytecode to running. (Once I’d checked out the repository)

STARTING HERE

In the /bin directory of the repository is a file called haxe-byte.exe. This is the version of haxe with the java target, compiled for windows. From the command line, you’re going to need to be able to call it.

I added its location to my PATH, but you could probably move it to somewhere that is already in your path, or reference it absolutely if you prefer.

Next, the HAXE_LIBRARY_PATH environmental variable needs to be set. You can do this in your instance of cmd.exe SET HAXE_LIBRARY_PATH=path\to\repo\haxe_ext\std, I just added it through system -> advanced settings -> environment variables

And then you’re set up to compile!

But I guess compiling is pretty useless without code. I started with a file called TestSource.hx

package;

class TestSource  {

static function main() {
new TestSource ();
}

public function new() {
trace("oh hi!");
}
}


To compile the haxe, I ran haxe-byte -main TestSource -java output_dir. This compiled TestSource, and placed the generated java files into output_dir/src

Now we’ve got a bunch of Java files! For me at least, not specifying a package meant my file ended up in the haxe.langdef package, and so came out as haxe/langdef/TestSource.java

To compile the java, you need the JDK. If you have that, first change directories into output_dir/src so the paths and package names work out correctly.
cd ouput_dir/src
javac haxe/langdef/TestSource.java

That should generate a bunch of class files alongside the java ones. All that’s left is to run the program!
java haxe/langdef/TestSource

That’s all!

Also, I set it up so that I could write haxe which compiles to java which nme used to create an APK for the android target, all of which I am pretty excited about! Hopefully I’ll write more about that setup tomorrow, and eventaully the haxe code behind it.