DEV Community

RobinFiveWords
RobinFiveWords

Posted on • Updated on

Getting packages to work when writing Java programs from scratch

In early 2023, to get better at reading and understanding Java code, I worked through Crafting Interpreters as intended, by typing every line of code. Well, almost every line. In the Java section of the book, each class file starts with this package statement:

package com.craftinginterpreters.lox;
Enter fullscreen mode Exit fullscreen mode

When I omitted that line and put all the Java source files in a single directory, everything compiled and ran as it should. When I tried to build a folder tree to match the package, it didn't work.

I'll be getting into Groovy soon, and I feel like I should solve this basic Java problem before I have to start learning about Gradle, Maven, Graven, and all other kinds of avens.

And...I've got it working now. I created a folder tree:

src
+-- com
    +-- craftinginterpreters
        +-- lox
            +-- *.java files
Enter fullscreen mode Exit fullscreen mode

Then I navigated to the src directory and compiled:

$ javac com/craftinginterpreters/lox/Lox.java
Enter fullscreen mode Exit fullscreen mode

And executed:

$ java j.lox.Lox
Enter fullscreen mode Exit fullscreen mode

What took quite a bit of time to troubleshoot tonight is that I initially copied some but not all of the source files. 😬

I'm still not sure why I couldn't get it working a year and a half ago. In any case, problem solved!

Top comments (0)