DEV Community

RobinFiveWords
RobinFiveWords

Posted on • Edited 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)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.