1
votes

I just began to learn Haxe, but I have encountered Compile error.

Main.hx

package helloworld;

import js.Lib;

class Main {

    static function main() {
        Lib.alert("Hello World");
    }

}

Please be careful about the target class is helloworld.Main.

build.hxml

-js bin/HelloWorld.js
-cp src
-main helloworld.Main
-debug

Build process log

Building HelloWorld_p140627
Running Pre-Build Command Line...
cmd: C:\HaxeToolkit\haxe\haxe.exe X:\tmp\HelloWorld_p140627\build.hxml
Class not found : helloworld.Main
Build halted with errors.
Done(1)

enter image description here

Why? The class helloworld.Main is surely exist. I cannot even say "hello, world"?

1

1 Answers

5
votes

Update now that I can see a screenshot of your project:

You are trying to compile "helloworld.Main", that means a class called "Main" in the package "helloworld", so Haxe will be looking for a file called "helloworld/Main.hx" in your "src/" directory.

However you have "src/Main.hx", not "src/helloworld/Main.hx". Create a subfolder called "helloworld", move "Main.hx" in there and you will be fine. The package you use in Haxe must match the directory structure.


Make sure your package aligns with your folders, and your file name with your class name. And all of these should be inside one of your "-cp" class path folders.

For your example above, the code looks fine, I would expect your layout to look like:

build.hxml                  <-- build file
src/helloworld/Main.hx      <-- classpath/package/class.Hx
bin/                        <-- output folder
bin/HelloWorld.js           <-- will be created once it compiles

And then you'd run haxe build.hxml. If that doesn't work for you please post the exact file structure of your project (which folders and which directories), and the command you use to build it, and the output.

Hope that helps,