Search code examples
javascalaclassloadersbt

Scala/Java subdirectories with dots in names


By some reasons I need to use long names of the packages in my project. Like:

com.example.foo.bar.bazz.anotherlongsubpart.andfinallytheactualpackagname

So considering the name convention(at least what I know about name convention in Java), I have to create appropriate directory structure replacing dots with slashes:

com/example/foo/bar/bazz/anotherlongsubpart/andfinallytheactualpackagname

As for me it looks too ugly :) So I tried instead to replace with slashes only the part of dots:

com.example.foo.bar.bazz.anotherlongsubpart/andfinallytheactualpackagname

In other words I have here one package/directory with long name "com.example.foo.bar.bazz.anotherlongsubpart" and another one with "andfinallytheactualpackagname". Is it legal to do that? I tried to compile all this stuff using Simple Build Tool, and run my unit tests on it. And it works well. As I would name it in more ordinary fashion with slashes.

So as my experiment shows it works well at least with SBT. But I'm not sure whether such practice may lead to further issues? Maybe in different environments with different kind of class loaders etc?


Solution

  • In Scala, the directory name does not have to map to the package declared at the source file, the compiler won't enforce this in any way.

    In Java, you can't do that. The source file must be at the correct directory (as dictated by the package name declared a the source file) for it to compile.

    So, if you're in Scala, you can do it as much as you like, in Java, no, you can't.