I'm not sure about how packages work in Haxe

First, this is my syntax for packages, based on Learn haxe in Y Minutes :

package src.Main;

class Main {

// code continues, and will compile with no errors when package is not used

I’m new to programming in general, and I was under the impression that the directory that the file is located in is on the left of the dot and a name we want to assign to our project goes on the right. However, when I try to compile in neko, using the terminal, I get this error:

Main.hx:1: characters 12-16 : Unexpected Userfiles

The name of the file is Main.hx and the project I wanted to create is Userfiles. I have tried both adding only the file directory…

Invalid commandline class : Main should be src.Main

…and replacing “Userfiles” with “Main”:

Main.hx:1: characters 12-16 : Unexpected Main

What error in syntax am I making that is causing a compiler warning?

The package should be in lower case, and is (basically) the directory structure.

For the file src/engine/utils/Helper.hx you’d have package src.engine.utils;.
If you wanted to use that file as main file you’d do haxe -main src.engine.utils.Main (...).

The only time the package isn’t exactly the directory structure is if you use classpaths,
by default haxe looks into the current directory for your code, but can also look in more.

For instance it’s often made that the src/ directory is added as classpath, by doing haxe -cp src/ (...),
in that case the file src/engine/utils/Helper.hx would have package engine.utils;, and be used by haxe -cp src/ -main engines.utils.Helper (...).

More advanced information in the manual: Modules and Paths - Haxe - The Cross-platform Toolkit

Thank you for helping but I don’t think I get it yet:

In Code Editor:
package src.engine.utils;

class Main {
  static function main() {
    trace("Hello, world");
  }
}

Directory structure: /home/myAccount/src/engine/utils/Main.hx

In terminal:
Case 1:
At “home/myAccount” level:
haxe -main src.engine.utils.Main -neko main.n
output:
Invalid commandline class : src.engine.utils.Main should be Main

Case 2:
cd to ~/src/engine/utils
haxe -main src.engine.utils.Main -neko main.n
output:
Type not found : src.engine.utils.Main

Am I getting errors because it is looking for other files in the directory (that don’t exist yet) and it’s not finding them?

Often what is done is you have a Main class in the topmost directory of your code folder, e.g. “src/Main.hx” which acts as the entry point. It would just have a package; line with no path. Then if you had other classes in subfolders you would use import statements to get them, and those classes would have the longer package statement at the top.

If you really want the Main entry point to be in the subfolders, you would want to use a .hxml file with the compiler instead of just a Main.hx file. Instructions for that are here. Does that help?

Thanks, it makes more sense to me now.

1 Like

You can of course use the same directives from the hxml file on the command line, but hxml is a nicer way to organise it.