Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to trigger an error/warning immediately upon compile/run if an item on my class-path is missing
I have a very simple source file -- HelloWorld.java
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("hello world");
}
}
I have the following compile command.
javac HelloWorld.java
If I run it, this works fine.
I have the following run command.
java HelloWorld
If I run it, this works fine too.
Now, I also have a single jar file (some.jar
) as a dependency. I am not using it yet, but I will be, and therefore, want to start putting it onto my class-path.
So, I tried to run the above 2 commands with a --class-path
included this time.
javac --class-path=".;some.jar" HelloWorld.java
java --class-path=".;some.jar" HelloWorld
This also worked.
Finally, I accidentally deleted my some.jar
, and reran the above 2 commands.
Surprisingly enough, there were no errors or warnings from Java.
My question is -- how do I get Java to tell me that it cannot find the item I have listed on my classpath? I don't really care if the warning is upon compile or upon run, but I would like to know I made a mistake some point before my program actually tries to use the dependency (and fail because Java couldn't find it this entire time).
Basically, I want some form of confirmation or reaffirmation that Java knows where my specified items on the class-path are, and can reach them.
Please recall, this is a minimal, reproducible example. My actual problem involves WAY more moving pieces. I'm using this (admittedly contrived) example as a way to highlight my point.
EDIT - To better explain why this is a problem, let's leave the minimal example and talk about my actual problem. I am in the middle of overhauling my codebase. I am trying to update dependencies, refactor code, reorganize my folder structure, and more. In the midst of this, I run into a ClassNotFoundException. The first time, it was because I had an out-of-date version of my dependency. But the second time, it was because I had put the jar file in the wrong location, and more specifically, the location that I had put on the class-path was pointing at a non-existent jar file location.
I want to nip this problem in the bud by having Java tell me upfront if the class-path location I am providing it is pointing to nothing at all. Not being able to do that hurts the maintainability of my codebase significantly during large refactors and overhauls.
EDIT 2 - If there is no other option besides using a build tool (Maven/Gradle/etc), then I am willing to consider that. But I want to emphasize, that is a worst-case scenario solution. My project is suffering from enough complexity as is, and the last thing I want to do is uproot by build solution from the ground up.
My goal is to make small, incremental changes all over my code base. All together, it adds up to a complete overhaul, but it's made up of a bunch of tiny, bite-sized steps. Layering on an entire build system to my codebase is the exact opposite of small and bite-sized. My original build solution works great, it just ran into this problem while refactoring.
0 comment threads