bld icon indicating copy to clipboard operation
bld copied to clipboard

Add an error message when running without compiled classes

Open Xasmedy opened this issue 3 months ago • 2 comments

In case the build-output directory is empty or does not exists, instead of trying to run the non-existent classes anyway, provide an error message stating that there are no .class files to run.

This is helpful because sometimes it happens to do a clean and forgetting the source is not compiled anymore, so when the run is issued, the java binary cannot find anything and will return an error message. This error message is quite confusing and takes a bit of mental overhead and time to figure out that there are simply no classes to run, and sometimes (at least in my case) it would lead the user to believe there's a configuration mistake, which is not the case.

Xasmedy avatar Nov 14 '25 18:11 Xasmedy

Seems pretty clear to me, ClassNotFoundException:

> ./bld run
Error: Could not find or load main class com.example.MyApp
Caused by: java.lang.ClassNotFoundException: com.example.MyApp

What more would you like to see?

ethauvin avatar Nov 14 '25 20:11 ethauvin

This is the error message when attempting to run a non-modular class:

Error: Could not find or load main class com.example.App
Caused by: java.lang.ClassNotFoundException: com.example.App

And this is the message while running a modular one:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module my.app not found

The non-modular one, even if a bit generic, can be feasible to understand, while the modular one, it's quite harder. With generic I mean that the java.lang.ClassNotFoundException can happen for many reasons:

  • The main class has been deleted.
  • There is a wrong mainClass configuration.
  • The build-path configuration is wrong.
  • There are no compiled classes in build.

One must check all of these to discover what is wrong, and usually we start from the most pessimistic. Having a Cannot run because there are no compiled sources., makes the problem specific, without having to spend time or worrying that something is broken. Then, having specific and helpful error messages that save on time and cognitive overhead make the software of higher quality.

Talking about the modular error message. It might make someone think that they did something wrong, and since the modular system has a reputation, there's a chance they'll just strip away the module-info.java, re-compile because they removed the file, and.. everything works fine.. (because now we have the compile classes) enforcing the idea that modules are broken, or just really hard.

From my experience, that message is quite common when working with modules, and it might actually be because of a configuration mistake or other problem. I got it so many times when I was debugging for this problem, the idea of giving up on modules came to mind, and I'm one of those Java devs that try to use them as much as possible, luckily, I'm stubborn and endured.

Xasmedy avatar Nov 15 '25 11:11 Xasmedy